mirror of
				https://gitlab.gnome.org/julianschacher/top-bar-organizer.git
				synced 2025-10-27 15:19:09 +00:00 
			
		
		
		
	Refactor: Merge AppIndi.KStat.ItemManager code into BoxOrderManager
				
					
				
			Let `BoxOrderManager` do the relevant work itself and get rid of `AppIndicatorKStatusNotifierItemManager`. This is also in preparation to make the addition of AppIndicator/KStatusNotifierItem items work again.
This commit is contained in:
		
							parent
							
								
									59d5665661
								
							
						
					
					
						commit
						644656d93a
					
				| @ -7,7 +7,6 @@ const Me = ExtensionUtils.getCurrentExtension(); | ||||
| const Main = imports.ui.main; | ||||
| const Panel = imports.ui.panel; | ||||
| 
 | ||||
| const AppIndicatorKStatusNotifierItemManager = Me.imports.extensionModules.AppIndicatorKStatusNotifierItemManager; | ||||
| const BoxOrderManager = Me.imports.extensionModules.BoxOrderManager; | ||||
| 
 | ||||
| class Extension { | ||||
| @ -17,11 +16,7 @@ class Extension { | ||||
|     enable() { | ||||
|         this.settings = ExtensionUtils.getSettings(); | ||||
| 
 | ||||
|         // Create an instance of AppIndicatorKStatusNotifierItemManager to
 | ||||
|         // handle AppIndicator/KStatusNotifierItem items.
 | ||||
|         this._appIndicatorKStatusNotifierItemManager = new AppIndicatorKStatusNotifierItemManager.AppIndicatorKStatusNotifierItemManager(); | ||||
| 
 | ||||
|         this._boxOrderManager = new BoxOrderManager.BoxOrderManager(this._appIndicatorKStatusNotifierItemManager); | ||||
|         this._boxOrderManager = new BoxOrderManager.BoxOrderManager(); | ||||
| 
 | ||||
|         // Stuff to do on startup(extension enable).
 | ||||
|         this._boxOrderManager.saveNewTopBarItems(); | ||||
|  | ||||
| @ -1,107 +0,0 @@ | ||||
| "use strict"; | ||||
| /* exported AppIndicatorKStatusNotifierItemManager */ | ||||
| 
 | ||||
| var AppIndicatorKStatusNotifierItemManager = class AppIndicatorKStatusNotifierItemManager { | ||||
|     constructor() { | ||||
|         // Create an application-role map for associating roles with
 | ||||
|         // applications.
 | ||||
|         // This is needed so that this class can handle/manage
 | ||||
|         // AppIndicator/KStatusNotifierItem items.
 | ||||
|         this._applicationRoleMap = new Map(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Handle an AppIndicator/KStatusNotifierItem item. | ||||
|      * | ||||
|      * This function basically does the following two things: | ||||
|      * - Associate the role of the given item with the application of the | ||||
|      *   AppIndicator/KStatusNotifierItem. | ||||
|      * - Add a placeholder for the roles associated with the application of the | ||||
|      *   AppIndiciator/KStatusNotifierItem to the box order, if needed. | ||||
|      * | ||||
|      * Note: The caller is responsible for saving the updated box order to | ||||
|      * settings. | ||||
|      * @param {} indicatorContainer - The container of the indicator of the | ||||
|      * AppIndicator/KStatusNotifierItem item. | ||||
|      * @param {string} role - The role of the AppIndicator/KStatusNotifierItem | ||||
|      * item. | ||||
|      * @param {string[]} - The box order the placeholder should be added to, if | ||||
|      * needed. | ||||
|      * @param {BoxOrders} boxOrders - An object containing the box orders, which | ||||
|      * is currently getting worked on. | ||||
|      * @param {boolean} - Whether to add the placeholder to the beginning of the | ||||
|      * box order. | ||||
|      */ | ||||
|     handleAppIndicatorKStatusNotifierItemItem(indicatorContainer, role, boxOrder, boxOrders, atToBeginning = false) { | ||||
|         // Get the application the AppIndicator/KStatusNotifierItem is
 | ||||
|         // associated with.
 | ||||
|         let application = indicatorContainer.get_child()._indicator.id; | ||||
| 
 | ||||
|         // Since the Dropbox client appends its PID to the id, drop the PID and
 | ||||
|         // the hyphen before it.
 | ||||
|         if (application.startsWith("dropbox-client-")) application = "dropbox-client"; | ||||
| 
 | ||||
|         // Associate the role with the application.
 | ||||
|         let roles = this._applicationRoleMap.get(application); | ||||
|         if (roles) { | ||||
|             // If the application already has an array of associated roles, just
 | ||||
|             // add the role to it, if needed.
 | ||||
|             if (!roles.includes(role)) roles.push(role); | ||||
|         } else { | ||||
|             // Otherwise create a new array.
 | ||||
|             this._applicationRoleMap.set(application, [ role ]); | ||||
|         } | ||||
| 
 | ||||
|         // Store a placeholder for the roles associated with the application in
 | ||||
|         // the box order, if needed.
 | ||||
|         // (Then later the `this.createResolvedBoxOrder` method can be used to
 | ||||
|         // get a box order, where the placeholder/s get/s replaced with the
 | ||||
|         // relevant roles (by using `this._applicationRoleMap`).)
 | ||||
|         const placeholder = `appindicator-kstatusnotifieritem-${application}`; | ||||
|         if (!boxOrders.left.includes(placeholder) | ||||
|             && !boxOrders.center.includes(placeholder) | ||||
|             && !boxOrders.right.includes(placeholder)) { | ||||
|             if (atToBeginning) { | ||||
|                 boxOrder.unshift(placeholder); | ||||
|             } else { | ||||
|                 boxOrder.push(placeholder); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * This function takes a box order and returns a box order, where all | ||||
|      * placeholders got replaced with their relevant roles. | ||||
|      * @param {string[]} boxOrder - The box order of which to replace the | ||||
|      * placeholders. | ||||
|      * @returns {string[]} A resolved box order, where all placeholders got | ||||
|      * replaced with their relevant roles. | ||||
|      */ | ||||
|     createResolvedBoxOrder(boxOrder) { | ||||
|         let resolvedBoxOrder = [ ]; | ||||
|         for (const item of boxOrder) { | ||||
|             // If the item isn't a placeholder, just add it to the new resolved
 | ||||
|             // box order.
 | ||||
|             if (!item.startsWith("appindicator-kstatusnotifieritem-")) { | ||||
|                 resolvedBoxOrder.push(item); | ||||
|                 continue; | ||||
|             } | ||||
| 
 | ||||
|             /// If the item is a placeholder, replace it.
 | ||||
|             // First get the application this placeholder is associated with.
 | ||||
|             const application = item.replace("appindicator-kstatusnotifieritem-", ""); | ||||
| 
 | ||||
|             // Then get the roles associated with the application.
 | ||||
|             let roles = this._applicationRoleMap.get(application); | ||||
| 
 | ||||
|             // Continue, if there are no roles.
 | ||||
|             if (!roles) continue; | ||||
|             // Otherwise add the roles
 | ||||
|             for (const role of roles) { | ||||
|                 resolvedBoxOrder.push(role); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return resolvedBoxOrder; | ||||
|     } | ||||
| }; | ||||
| @ -21,18 +21,86 @@ const Main = imports.ui.main; | ||||
|  * It's basically a heavy wrapper around the box orders stored in the settings. | ||||
|  */ | ||||
| var BoxOrderManager = class BoxOrderManager { | ||||
|     /** | ||||
|      * @param {AppIndicatorKStatusNotifierItemManager} | ||||
|      * appIndicatorKStatusNotifierItemManager - An instance of | ||||
|      * AppIndicatorKStatusNotifierItemManager to be used in the methods of | ||||
|      * `this`. | ||||
|      */ | ||||
|     constructor(appIndicatorKStatusNotifierItemManager) { | ||||
|         this._appIndicatorKStatusNotifierItemManager = appIndicatorKStatusNotifierItemManager; | ||||
|     constructor() { | ||||
|         this._appIndicatorItemApplicationRoleMap = new Map(); | ||||
| 
 | ||||
|         this._settings = ExtensionUtils.getSettings(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Handles an AppIndicator/KStatusNotifierItem item by associating the role | ||||
|      * of the given item with the application of the | ||||
|      * AppIndicator/KStatusNotifier item and returning a placeholder role. | ||||
|      * @param {string} indicatorContainer - The container of the indicator of the | ||||
|      * AppIndicator/KStatusNotifierItem item. | ||||
|      * @param {string} role - The role of the AppIndicator/KStatusNotifierItem | ||||
|      * item. | ||||
|      * @returns {string} The placeholder role. | ||||
|      */ | ||||
|     #handleAppIndicatorItem(indicatorContainer, role) { | ||||
|         let application = indicatorContainer.get_child()._indicator.id; | ||||
| 
 | ||||
|         // Since the Dropbox client appends its PID to the id, drop the PID and
 | ||||
|         // the hyphen before it.
 | ||||
|         if (application.startsWith("dropbox-client-")) { | ||||
|             application = "dropbox-client"; | ||||
|         } | ||||
| 
 | ||||
|         // Associate the role with the application.
 | ||||
|         let roles = this._appIndicatorItemApplicationRoleMap.get(application); | ||||
|         if (roles) { | ||||
|             // If the application already has an array of associated roles, just
 | ||||
|             // add the role to it, if needed.
 | ||||
|             if (!roles.includes(role)) { | ||||
|                 roles.push(role); | ||||
|             } | ||||
|         } else { | ||||
|             // Otherwise create a new array.
 | ||||
|             this._appIndicatorItemApplicationRoleMap.set(application, [ role ]); | ||||
|         } | ||||
| 
 | ||||
|         // Return the placeholder.
 | ||||
|         // A box order containing this placeholder can later be resolved to
 | ||||
|         // relevant roles using `#resolveAppIndicatorPlaceholders`.
 | ||||
|         return `appindicator-kstatusnotifieritem-${application}`; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Takes a box order and replaces AppIndicator placeholder roles with | ||||
|      * actual roles. | ||||
|      * @param {string[]} - The box order of which to replace placeholder roles. | ||||
|      * @returns {string[]} - A box order with all placeholder roles | ||||
|      * resolved/replaced to/with actual roles. | ||||
|      */ | ||||
|     #resolveAppIndicatorPlaceholders(boxOrder) { | ||||
|         let resolvedBoxOrder = [ ]; | ||||
|         for (const role of boxOrder) { | ||||
|             // If the role isn't a placeholder, just add it to the resolved box
 | ||||
|             // order.
 | ||||
|             if (!role.startsWith("appindicator-kstatusnotifieritem-")) { | ||||
|                 resolvedBoxOrder.push(role); | ||||
|                 continue; | ||||
|             } | ||||
| 
 | ||||
|             /// If the role is a placeholder, replace it.
 | ||||
|             // First get the application this placeholder is associated with.
 | ||||
|             const application = role.replace("appindicator-kstatusnotifieritem-", ""); | ||||
| 
 | ||||
|             // Then get the actual roles associated with this application.
 | ||||
|             let actualRoles = this._appIndicatorItemApplicationRoleMap.get(application); | ||||
| 
 | ||||
|             // If there are no actual roles, continue.
 | ||||
|             if (!actualRoles) { | ||||
|                 continue; | ||||
|             } | ||||
| 
 | ||||
|             // Otherwise add the actual roles to the resolved box order.
 | ||||
|             resolvedBoxOrder.push(...actualRoles); | ||||
|         } | ||||
| 
 | ||||
|         return resolvedBoxOrder; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * This method returns a valid box order for the given top bar box. | ||||
|      * This means it returns a box order, where only roles are included, which | ||||
| @ -47,7 +115,7 @@ var BoxOrderManager = class BoxOrderManager { | ||||
|      */ | ||||
|     createValidBoxOrder(box) { | ||||
|         // Get a resolved box order.
 | ||||
|         let boxOrder = this._appIndicatorKStatusNotifierItemManager.createResolvedBoxOrder(this._settings.get_strv(`${box}-box-order`)); | ||||
|         let boxOrder = this.#resolveAppIndicatorPlaceholders(this._settings.get_strv(`${box}-box-order`)); | ||||
| 
 | ||||
|         // ToDo: simplify.
 | ||||
|         // Get the indicator containers (of the items) currently present in the
 | ||||
| @ -111,13 +179,12 @@ var BoxOrderManager = class BoxOrderManager { | ||||
|             for (const indicatorContainer of indicatorContainers) { | ||||
|                 // First get the role associated with the current indicator
 | ||||
|                 // container.
 | ||||
|                 const role = indicatorContainerRoleMap.get(indicatorContainer); | ||||
|                 let role = indicatorContainerRoleMap.get(indicatorContainer); | ||||
|                 if (!role) continue; | ||||
| 
 | ||||
|                 // Handle an AppIndicator/KStatusNotifierItem item differently.
 | ||||
|                 if (role.startsWith("appindicator-")) { | ||||
|                     this._appIndicatorKStatusNotifierItemManager.handleAppIndicatorKStatusNotifierItemItem(indicatorContainer, role, boxOrder, boxOrders, box === "right"); | ||||
|                     continue; | ||||
|                     role = this.#handleAppIndicatorItem(indicatorContainer, role); | ||||
|                 } | ||||
| 
 | ||||
|                 // Add the role to the box order, if it isn't in in one already.
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user