-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor preferences window code (#94)
* refactor for a nicer code * split perf win in its own file * more refactor * small refactor * refactor behaviorComboRow * more refactor * more refactor * more refactor * small clean up
- Loading branch information
Showing
3 changed files
with
159 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { Adw1_ as Adw_, Gtk4_ as Gtk_, python } from "deno-gtk-py"; | ||
import { UI_LABELS } from "./consts.ts"; | ||
import { Indicator } from "./indicator/indicator_api.ts"; | ||
import { Adw, GLib, Gtk, MainWindow } from "./main.ts"; | ||
import { ComboItemManager } from "./utils.ts"; | ||
|
||
export type Theme = "System Theme" | "Light" | "Dark"; | ||
export type Behavior = "Ask Confirmation" | "Run in Background" | "Quit"; | ||
|
||
export class PreferencesMenu { | ||
#preferencesWin: Adw_.PreferencesWindow; | ||
#themeItems = new ComboItemManager<Theme>(["System Theme", "Light", "Dark"]); | ||
#behaviorOnExitItems = new ComboItemManager<Behavior>([ | ||
"Ask Confirmation", | ||
"Run in Background", | ||
"Quit", | ||
]); | ||
|
||
constructor(mainWindow: MainWindow) { | ||
const builder = Gtk.Builder(); | ||
builder.add_from_file( | ||
new URL(import.meta.resolve("./ui/preferences.ui")).pathname, | ||
); | ||
|
||
this.#preferencesWin = builder.get_object( | ||
"preferencesWin", | ||
) as Adw_.PreferencesWindow; | ||
this.#preferencesWin.set_hide_on_close(true); | ||
this.#preferencesWin.set_modal(true); | ||
|
||
const themeRow = builder.get_object<Adw_.ComboRow>("themeRow"); | ||
|
||
themeRow.set_title(UI_LABELS.Theme); | ||
themeRow.set_model(Gtk.StringList.new(this.#themeItems.itemsTranslated)); | ||
//NOTE: ADW bug, set_selected(0) doesn't set the item as selected initilally | ||
// so trigger it with this, before the actual correct selection | ||
themeRow.set_selected(1); | ||
themeRow.set_selected(this.#themeItems.toId(mainWindow.state["themeV2"])); | ||
themeRow.connect( | ||
"notify::selected", | ||
python.callback(() => { | ||
const theme = this.#themeItems.fromId( | ||
themeRow.get_selected().valueOf(), | ||
); | ||
//deno-fmt-ignore | ||
Adw.StyleManager.get_default().set_color_scheme( | ||
theme === "System Theme" ? Adw.ColorScheme.DEFAULT | ||
: theme === "Light" ? Adw.ColorScheme.FORCE_LIGHT | ||
: Adw.ColorScheme.FORCE_DARK, | ||
); | ||
mainWindow.updateState({ "themeV2": theme }); | ||
}), | ||
); | ||
|
||
const behaviorOnExitRow = builder.get_object( | ||
"behaviorOnExitRow", | ||
) as Adw_.ComboRow; | ||
behaviorOnExitRow.set_title(UI_LABELS["Behavior on Closing"]); | ||
behaviorOnExitRow.set_subtitle(UI_LABELS["Applies only while active"]); | ||
behaviorOnExitRow.set_model( | ||
Gtk.StringList.new(this.#behaviorOnExitItems.itemsTranslated), | ||
); | ||
//NOTE: ADW bug, set_selected(0) doesn't set the item as selected initilally | ||
// so trigger it with this, before the actual correct selection | ||
behaviorOnExitRow.set_selected(1); | ||
behaviorOnExitRow.set_selected( | ||
this.#behaviorOnExitItems.toId(mainWindow.state["exitBehaviorV2"]), | ||
); | ||
|
||
behaviorOnExitRow.connect( | ||
"notify::selected", | ||
python.callback(() => { | ||
const behavior = this.#behaviorOnExitItems.fromId( | ||
behaviorOnExitRow | ||
.get_selected().valueOf(), | ||
); | ||
// If the option is a `Run In Background` make sure to run the indicator | ||
if (behavior === "Run in Background") { | ||
if (mainWindow.indicator === undefined) { | ||
mainWindow.indicator = new Indicator(mainWindow); | ||
} | ||
if (mainWindow.state["suspend"]) { | ||
mainWindow.indicator.activate(); | ||
} else { | ||
mainWindow.indicator.deactivate(); | ||
} | ||
} else { | ||
// NOTE: run this after a bit of time, so messages don't get mixed up in the write buffer | ||
GLib.timeout_add( | ||
500, | ||
python.callback(() => mainWindow.indicator?.hide()), | ||
); | ||
} | ||
|
||
mainWindow.updateState({ "exitBehaviorV2": behavior }); | ||
}), | ||
); | ||
} | ||
|
||
set_transient_for(window: Gtk_.ApplicationWindow) { | ||
this.#preferencesWin.set_transient_for(window); | ||
} | ||
present() { | ||
this.#preferencesWin.set_visible(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { UI_LABELS } from "./consts.ts"; | ||
|
||
/** Utility class for working with Gtk combo items */ | ||
export class ComboItemManager<T> { | ||
items: T[]; | ||
|
||
constructor(items: T[]) { | ||
this.items = items; | ||
} | ||
|
||
get itemsTranslated(): string[] { | ||
return this.items.map((item) => UI_LABELS[item as keyof UI_LABELS]); | ||
} | ||
|
||
fromId(id: number): T { | ||
if (id < 0 || id >= this.items.length) { | ||
throw new Error(`Invalid item ID: ${id}`); | ||
} | ||
return this.items[id]; | ||
} | ||
|
||
toId(item: T): number { | ||
return this.items.indexOf(item); | ||
} | ||
} |