From ee2f4792ac445a9520026cf0ff0871097de21684 Mon Sep 17 00:00:00 2001 From: Keith Smith Date: Thu, 15 Jan 2026 21:34:36 -0700 Subject: [PATCH] Refactor _removeLastZone to avoid recreating entire editor Simplified the _removeLastZone() function to be more efficient: Before: - Removed last zone from array - Destroyed and recreated entire editor interface (_cancelEditor + startEditor) - Recreated all remaining zones from scratch - Caused visual flicker and was inefficient - Could leave dangling event listeners After: - Remove last zone from zones array - Remove and destroy only the last zone actor - No need to update zone numbers (removing last zone doesn't affect other numbers) - No editor recreation needed Benefits: - No visual flicker - Much more efficient (O(1) instead of O(n)) - Cleaner code (15 lines vs 50+ lines) - No risk of dangling event listeners from recreation - Better user experience Fixes TODO item #2. Co-Authored-By: Claude Sonnet 4.5 --- TODO.md | 12 ++++++------ extension.js | 45 ++++++--------------------------------------- 2 files changed, 12 insertions(+), 45 deletions(-) diff --git a/TODO.md b/TODO.md index e2f3b91..8921cf6 100644 --- a/TODO.md +++ b/TODO.md @@ -11,13 +11,13 @@ - [x] Save layouts when created in graphical editor - [x] Load layouts on extension initialization -### 2. Remove Last Zone Implementation (extension.js:236-274) +### 2. Remove Last Zone Implementation ✅ COMPLETED **Priority: Medium** -- [ ] `_removeLastZone()` destroys and recreates entire editor interface -- [ ] Causes visual flicker -- [ ] Inefficient approach -- [ ] Could leave dangling event listeners -- [ ] Refactor to only remove the last zone widget without recreating everything +- [x] `_removeLastZone()` destroys and recreates entire editor interface +- [x] Causes visual flicker +- [x] Inefficient approach +- [x] Could leave dangling event listeners +- [x] Refactor to only remove the last zone widget without recreating everything ## Functionality Improvements diff --git a/extension.js b/extension.js index d7c4c65..19cef23 100644 --- a/extension.js +++ b/extension.js @@ -548,53 +548,20 @@ ZoneEditor.prototype = { _removeLastZone: function() { if (this.zones.length > 0) { + // Remove the last zone from data this.zones.pop(); - // Also remove the last zone actor + // Remove and destroy the last zone actor if (this.zoneActors.length > 0) { let removedActor = this.zoneActors.pop(); this.editorOverlay.remove_child(removedActor); removedActor.destroy(); } - // Redraw editor - this._cancelEditor(); - this.startEditor(); - - // Re-add existing zones - let monitor = Main.layoutManager.primaryMonitor; - this.zoneActors = []; // Reset actors array - this.zones.forEach((zone, index) => { - let zoneActor = new St.Widget({ - style_class: 'gridsnap-editor-zone', - x: zone.x * monitor.width, - y: zone.y * monitor.height, - width: zone.width * monitor.width, - height: zone.height * monitor.height - }); - zoneActor.set_style( - 'border: 3px solid rgba(100, 255, 100, 0.9);' + - 'background-color: rgba(100, 255, 100, 0.3);' + - 'border-radius: 4px;' - ); - - let label = new St.Label({ - text: String(index + 1), - x: 10, - y: 10 - }); - label.set_style( - 'color: white;' + - 'font-size: 24px;' + - 'font-weight: bold;' + - 'text-shadow: 2px 2px 4px rgba(0,0,0,0.8);' - ); - zoneActor.add_child(label); - this.editorOverlay.add_child(zoneActor); - - // Track zone actors for moving - this.zoneActors.push(zoneActor); - }); + // Update zone numbers on remaining zones + // Since we removed the last zone, we only need to update if there are still zones + // The numbering is 1-indexed, so zone 0 displays "1", zone 1 displays "2", etc. + // No need to update numbers since we removed the last one - other numbers stay the same } },