From dd67cbefeb810d16e5251866ec18ead4847bd980 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Tue, 5 Aug 2025 09:25:09 +1000 Subject: [PATCH] Perfect! I've successfully refactored the code to have a common `jumplist.go` file with all the shared structs and methods. This eliminates code duplication across platforms. Here's what the refactoring accomplished: ## Summary of Refactoring ### 1. **Common File** (`jumplist.go`) - Contains all shared structs: `JumpListItemType`, `JumpListItem`, `JumpListCategory`, and `JumpList` - Implements common methods: `AddCategory()`, `ClearCategories()`, and `Apply()` - The `Apply()` method calls the platform-specific `applyPlatform()` method ### 2. **Platform-Specific Files** - **Windows** (`jumplist_windows.go`): Contains only Windows-specific implementation - COM interface definitions and implementations - `CreateJumpList()` method for windowsApp - `applyPlatform()` method with the actual Windows jumplist logic - **macOS** (`jumplist_darwin.go`): Minimal stub implementation - `CreateJumpList()` method for darwinApp - `applyPlatform()` method that returns nil (no-op) - **Linux** (`jumplist_linux.go`): Minimal stub implementation - `CreateJumpList()` method for linuxApp - `applyPlatform()` method that returns nil (no-op) ### Benefits - **No code duplication**: All common structs and logic are in one place - **Cleaner platform files**: Platform-specific files only contain platform-specific code - **Easier maintenance**: Changes to the API only need to be made in one place - **Type safety**: The common interface ensures all platforms implement the required methods The API remains exactly the same for users, but the implementation is now much cleaner and more maintainable. --- v3/pkg/application/jumplist.go | 55 ++++++++++++++++++++++++++ v3/pkg/application/jumplist_darwin.go | 39 +----------------- v3/pkg/application/jumplist_linux.go | 39 +----------------- v3/pkg/application/jumplist_windows.go | 43 ++++---------------- 4 files changed, 64 insertions(+), 112 deletions(-) create mode 100644 v3/pkg/application/jumplist.go diff --git a/v3/pkg/application/jumplist.go b/v3/pkg/application/jumplist.go new file mode 100644 index 000000000..0c33f77f5 --- /dev/null +++ b/v3/pkg/application/jumplist.go @@ -0,0 +1,55 @@ +package application + +// JumpListItemType represents the type of jump list item +type JumpListItemType int + +const ( + // JumpListItemTypeTask represents a task item + JumpListItemTypeTask JumpListItemType = iota + // JumpListItemTypeSeparator represents a separator (Windows only) + JumpListItemTypeSeparator +) + +// JumpListItem represents a single item in a jump list +type JumpListItem struct { + Type JumpListItemType + Title string + Description string + FilePath string + Arguments string + IconPath string + IconIndex int +} + +// JumpListCategory represents a category of items in a jump list +type JumpListCategory struct { + Name string + Items []JumpListItem +} + +// JumpList provides an interface for managing application jump lists. +// This is primarily a Windows feature, but the API is designed to be +// cross-platform safe (no-op on non-Windows platforms). +type JumpList struct { + app platformApp + categories []JumpListCategory +} + +// AddCategory adds a category to the jump list. +// If the category name is empty, the items will be added as tasks. +func (j *JumpList) AddCategory(category JumpListCategory) { + j.categories = append(j.categories, category) +} + +// ClearCategories removes all categories from the jump list. +func (j *JumpList) ClearCategories() { + j.categories = []JumpListCategory{} +} + +// Apply applies the current jump list configuration. +// On Windows, this updates the taskbar jump list. +// On other platforms, this is a no-op. +func (j *JumpList) Apply() error { + // Platform-specific implementation + return j.applyPlatform() +} \ No newline at end of file diff --git a/v3/pkg/application/jumplist_darwin.go b/v3/pkg/application/jumplist_darwin.go index d2e524923..23f1db048 100644 --- a/v3/pkg/application/jumplist_darwin.go +++ b/v3/pkg/application/jumplist_darwin.go @@ -2,33 +2,6 @@ package application -type JumpListItemType int - -const ( - JumpListItemTypeTask JumpListItemType = iota - JumpListItemTypeSeparator -) - -type JumpListItem struct { - Type JumpListItemType - Title string - Description string - FilePath string - Arguments string - IconPath string - IconIndex int -} - -type JumpListCategory struct { - Name string - Items []JumpListItem -} - -type JumpList struct { - app *darwinApp - categories []JumpListCategory -} - func (app *darwinApp) CreateJumpList() *JumpList { return &JumpList{ app: app, @@ -36,17 +9,7 @@ func (app *darwinApp) CreateJumpList() *JumpList { } } -func (j *JumpList) AddCategory(category JumpListCategory) { - // Stub implementation for macOS - j.categories = append(j.categories, category) -} - -func (j *JumpList) ClearCategories() { - // Stub implementation for macOS - j.categories = []JumpListCategory{} -} - -func (j *JumpList) Apply() error { +func (j *JumpList) applyPlatform() error { // Stub implementation for macOS // Jump lists are Windows-specific, so this is a no-op on macOS return nil diff --git a/v3/pkg/application/jumplist_linux.go b/v3/pkg/application/jumplist_linux.go index 29c2845e3..c408b00dd 100644 --- a/v3/pkg/application/jumplist_linux.go +++ b/v3/pkg/application/jumplist_linux.go @@ -2,33 +2,6 @@ package application -type JumpListItemType int - -const ( - JumpListItemTypeTask JumpListItemType = iota - JumpListItemTypeSeparator -) - -type JumpListItem struct { - Type JumpListItemType - Title string - Description string - FilePath string - Arguments string - IconPath string - IconIndex int -} - -type JumpListCategory struct { - Name string - Items []JumpListItem -} - -type JumpList struct { - app *linuxApp - categories []JumpListCategory -} - func (app *linuxApp) CreateJumpList() *JumpList { return &JumpList{ app: app, @@ -36,17 +9,7 @@ func (app *linuxApp) CreateJumpList() *JumpList { } } -func (j *JumpList) AddCategory(category JumpListCategory) { - // Stub implementation for Linux - j.categories = append(j.categories, category) -} - -func (j *JumpList) ClearCategories() { - // Stub implementation for Linux - j.categories = []JumpListCategory{} -} - -func (j *JumpList) Apply() error { +func (j *JumpList) applyPlatform() error { // Stub implementation for Linux // Jump lists are Windows-specific, so this is a no-op on Linux return nil diff --git a/v3/pkg/application/jumplist_windows.go b/v3/pkg/application/jumplist_windows.go index eff3aeb79..2891a17db 100644 --- a/v3/pkg/application/jumplist_windows.go +++ b/v3/pkg/application/jumplist_windows.go @@ -9,33 +9,6 @@ import ( "unsafe" ) -type JumpListItemType int - -const ( - JumpListItemTypeTask JumpListItemType = iota - JumpListItemTypeSeparator -) - -type JumpListItem struct { - Type JumpListItemType - Title string - Description string - FilePath string - Arguments string - IconPath string - IconIndex int -} - -type JumpListCategory struct { - Name string - Items []JumpListItem -} - -type JumpList struct { - app *windowsApp - categories []JumpListCategory -} - var ( modole32 = syscall.NewLazyDLL("ole32.dll") modshell32 = syscall.NewLazyDLL("shell32.dll") @@ -169,15 +142,13 @@ func (app *windowsApp) CreateJumpList() *JumpList { } } -func (j *JumpList) AddCategory(category JumpListCategory) { - j.categories = append(j.categories, category) -} +func (j *JumpList) applyPlatform() error { + // Type assert to get the windowsApp + app, ok := j.app.(*windowsApp) + if !ok { + return fmt.Errorf("invalid app type for Windows jumplist") + } -func (j *JumpList) ClearCategories() { - j.categories = []JumpListCategory{} -} - -func (j *JumpList) Apply() error { hr := w32.CoInitializeEx(0, w32.COINIT_APARTMENTTHREADED) if hr != w32.S_OK && hr != w32.S_FALSE { return fmt.Errorf("CoInitializeEx failed: %v", hr) @@ -197,7 +168,7 @@ func (j *JumpList) Apply() error { } defer pDestList.Release() - appID := w32.MustStringToUTF16Ptr(j.app.parent.options.Name) + appID := w32.MustStringToUTF16Ptr(app.parent.options.Name) hr = pDestList.SetAppID(appID) if hr != w32.S_OK {