File indexing completed on 2024-11-10 04:57:11

0001 /*
0002     This file is part of the KDE project.
0003 
0004     SPDX-FileCopyrightText: 2013 Martin Gräßlin <mgraesslin@kde.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 /*global effect, effects, animate, cancel, set, animationTime, Effect, QEasingCurve */
0009 
0010 "use strict";
0011 
0012 var translucencyEffect = {
0013     activeWindow: effects.activeWindow,
0014     settings: {
0015         duration: animationTime(250),
0016         moveresize: 100,
0017         dialogs: 100,
0018         inactive: 100,
0019         comboboxpopups: 100,
0020         menus: 100,
0021         dropdownmenus: 100,
0022         popupmenus: 100,
0023         tornoffmenus: 100
0024     },
0025     loadConfig: function () {
0026         var i, individualMenu, windows;
0027         // TODO: add animation duration
0028         translucencyEffect.settings.moveresize     = effect.readConfig("MoveResize", 80);
0029         translucencyEffect.settings.dialogs        = effect.readConfig("Dialogs", 100);
0030         translucencyEffect.settings.inactive       = effect.readConfig("Inactive", 100);
0031         translucencyEffect.settings.comboboxpopups = effect.readConfig("ComboboxPopups", 100);
0032         translucencyEffect.settings.menus          = effect.readConfig("Menus", 100);
0033         individualMenu = effect.readConfig("IndividualMenuConfig", false);
0034         if (individualMenu === true) {
0035             translucencyEffect.settings.dropdownmenus = effect.readConfig("DropdownMenus", 100);
0036             translucencyEffect.settings.popupmenus    = effect.readConfig("PopupMenus", 100);
0037             translucencyEffect.settings.tornoffmenus  = effect.readConfig("TornOffMenus", 100);
0038         } else {
0039             translucencyEffect.settings.dropdownmenus = translucencyEffect.settings.menus;
0040             translucencyEffect.settings.popupmenus    = translucencyEffect.settings.menus;
0041             translucencyEffect.settings.tornoffmenus  = translucencyEffect.settings.menus;
0042         }
0043 
0044         windows = effects.stackingOrder;
0045         for (i = 0; i < windows.length; i += 1) {
0046             // stop all existing animations
0047             translucencyEffect.cancelAnimations(windows[i]);
0048             // schedule new animations based on new settings
0049             translucencyEffect.startAnimation(windows[i]);
0050             if (windows[i] !== effects.activeWindow) {
0051                 translucencyEffect.inactive.animate(windows[i]);
0052             }
0053         }
0054     },
0055     /**
0056      * @brief Starts the set animations depending on window type
0057      *
0058      */
0059     startAnimation: function (window) {
0060         var checkWindow = function (window, value) {
0061             if (value !== 100) {
0062                 var ids = set({
0063                     window: window,
0064                     duration: 1,
0065                     animations: [{
0066                         type: Effect.Opacity,
0067                         from: value / 100.0,
0068                         to: value / 100.0
0069                     }]
0070                 });
0071                 if (window.translucencyWindowTypeAnimation !== undefined) {
0072                     cancel(window.translucencyWindowTypeAnimation);
0073                 }
0074                 window.translucencyWindowTypeAnimation = ids;
0075             }
0076         };
0077         if (window.desktopWindow === true || window.dock === true || window.visible === false) {
0078             return;
0079         }
0080         if (window.dialog === true) {
0081             checkWindow(window, translucencyEffect.settings.dialogs);
0082         } else if (window.dropdownMenu === true) {
0083             checkWindow(window, translucencyEffect.settings.dropdownmenus);
0084         } else if (window.popupMenu === true) {
0085             checkWindow(window, translucencyEffect.settings.popupmenus);
0086         } else if (window.comboBox === true) {
0087             checkWindow(window, translucencyEffect.settings.comboboxpopups);
0088         } else if (window.menu === true) {
0089             checkWindow(window, translucencyEffect.settings.tornoffmenus);
0090         }
0091     },
0092     /**
0093      * @brief Cancels all animations for window type and inactive window
0094      *
0095      */
0096     cancelAnimations: function (window) {
0097         if (window.translucencyWindowTypeAnimation !== undefined) {
0098             cancel(window.translucencyWindowTypeAnimation);
0099             window.translucencyWindowTypeAnimation = undefined;
0100         }
0101         if (window.translucencyInactiveAnimation !== undefined) {
0102             cancel(window.translucencyInactiveAnimation);
0103             window.translucencyInactiveAnimation = undefined;
0104         }
0105         if (window.translucencyMoveResizeAnimations !== undefined) {
0106             cancel(window.translucencyMoveResizeAnimations);
0107             window.translucencyMoveResizeAnimations = undefined;
0108         }
0109     },
0110     moveResize: {
0111         start: function (window) {
0112             var ids;
0113             if (translucencyEffect.settings.moveresize === 100) {
0114                 return;
0115             }
0116             ids = set({
0117                 window: window,
0118                 duration: translucencyEffect.settings.duration,
0119                 animations: [{
0120                     type: Effect.Opacity,
0121                     to: translucencyEffect.settings.moveresize / 100.0
0122                 }]
0123             });
0124             window.translucencyMoveResizeAnimations = ids;
0125         },
0126         finish: function (window) {
0127             if (window.translucencyMoveResizeAnimations !== undefined) {
0128                 // start revert animation
0129                 animate({
0130                     window: window,
0131                     duration: translucencyEffect.settings.duration,
0132                     animations: [{
0133                         type: Effect.Opacity,
0134                         from: translucencyEffect.settings.moveresize / 100.0
0135                     }]
0136                 });
0137                 // and cancel previous animation
0138                 cancel(window.translucencyMoveResizeAnimations);
0139                 window.translucencyMoveResizeAnimations = undefined;
0140             }
0141         }
0142     },
0143     inactive: {
0144         activated: function (window) {
0145             if (translucencyEffect.settings.inactive === 100) {
0146                 return;
0147             }
0148             translucencyEffect.inactive.animate(translucencyEffect.activeWindow);
0149             translucencyEffect.activeWindow = window;
0150             if (window === null) {
0151                 return;
0152             }
0153             if (window.translucencyInactiveAnimation !== undefined) {
0154                 // start revert animation
0155                 animate({
0156                     window: window,
0157                     duration: translucencyEffect.settings.duration,
0158                     animations: [{
0159                         type: Effect.Opacity,
0160                         from: translucencyEffect.settings.inactive / 100.0
0161                     }]
0162                 });
0163                 // and cancel previous animation
0164                 cancel(window.translucencyInactiveAnimation);
0165                 window.translucencyInactiveAnimation = undefined;
0166             }
0167         },
0168         animate: function (window) {
0169             var ids;
0170             if (translucencyEffect.settings.inactive === 100) {
0171                 return;
0172             }
0173             if (window === null) {
0174                 return;
0175             }
0176             if (window === effects.activeWindow ||
0177                     window.popup === true ||
0178                     window.managed === false ||
0179                     window.desktopWindow === true ||
0180                     window.dock === true ||
0181                     window.visible === false ||
0182                     window.deleted === true) {
0183                 return;
0184             }
0185             ids = set({
0186                 window: window,
0187                 duration: translucencyEffect.settings.duration,
0188                 animations: [{
0189                     type: Effect.Opacity,
0190                     to: translucencyEffect.settings.inactive / 100.0
0191                 }]
0192             });
0193             window.translucencyInactiveAnimation = ids;
0194         }
0195     },
0196     desktopChanged: function () {
0197         var i, windows;
0198         windows = effects.stackingOrder;
0199         for (i = 0; i < windows.length; i += 1) {
0200             translucencyEffect.cancelAnimations(windows[i]);
0201             translucencyEffect.startAnimation(windows[i]);
0202             if (windows[i] !== effects.activeWindow) {
0203                 translucencyEffect.inactive.animate(windows[i]);
0204             }
0205         }
0206     },
0207     manage: function (window) {
0208         window.windowDesktopsChanged.connect(translucencyEffect.cancelAnimations);
0209         window.windowDesktopsChanged.connect(translucencyEffect.startAnimation);
0210         window.windowStartUserMovedResized.connect(translucencyEffect.moveResize.start);
0211         window.windowFinishUserMovedResized.connect(translucencyEffect.moveResize.finish);
0212 
0213         window.minimizedChanged.connect(() => {
0214             if (window.minimized) {
0215                 translucencyEffect.cancelAnimations();
0216             } else {
0217                 translucencyEffect.startAnimation(window);
0218                 translucencyEffect.inactive.animate(window);
0219             }
0220         });
0221     },
0222     init: function () {
0223         effect.configChanged.connect(translucencyEffect.loadConfig);
0224         effects.windowAdded.connect(translucencyEffect.manage);
0225         effects.windowAdded.connect(translucencyEffect.startAnimation);
0226         effects.windowClosed.connect(translucencyEffect.cancelAnimations);
0227         effects.windowActivated.connect(translucencyEffect.inactive.activated);
0228         effects.desktopChanged.connect(translucencyEffect.desktopChanged);
0229 
0230         for (const window of effects.stackingOrder) {
0231             translucencyEffect.manage(window);
0232         }
0233 
0234         translucencyEffect.loadConfig();
0235     }
0236 };
0237 translucencyEffect.init();