Skip to content

Commit

Permalink
Add Always On Top setting for Tauri
Browse files Browse the repository at this point in the history
This adds support for the `Always On Top` setting in the Tauri version
of LiveSplit One. This setting allows you to keep the LiveSplit One
window on top of all other windows.
  • Loading branch information
CryZe committed Jul 17, 2024
1 parent 4c078c1 commit 20e73d6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ fn resolve_hotkey(state: tauri::State<'_, State>, key_code: String) -> Cow<'stat
key_code.into()
}

#[tauri::command]
fn settings_changed(state: tauri::State<'_, State>, always_on_top: bool) {
if let Some(window) = &*state.window.read().unwrap() {
window.set_always_on_top(always_on_top).unwrap();
}
}

#[derive(Clone)]
struct TauriCommandSink(Arc<RwLock<Option<Window>>>);

Expand Down Expand Up @@ -219,6 +226,7 @@ fn main() {
set_hotkey_activation,
get_hotkey_config,
resolve_hotkey,
settings_changed,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
1 change: 1 addition & 0 deletions src/storage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ export async function loadGeneralSettings(): Promise<GeneralSettings> {
speedrunComIntegration: generalSettings.speedrunComIntegration ?? true,
splitsIoIntegration: generalSettings.splitsIoIntegration ?? true,
serverUrl: generalSettings.serverUrl,
alwaysOnTop: generalSettings.alwaysOnTop ?? (isTauri ? true : undefined),
};
}

Expand Down
19 changes: 13 additions & 6 deletions src/ui/LiveSplit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ export class LiveSplit extends React.Component<Props, State> {
layoutModified: false,
};

if (window.__TAURI__ != null) {
window.__TAURI__.event.listen("command", (event) => {
const payloadString = JSON.stringify(event.payload);
ServerProtocol.handleCommand(payloadString, commandSink.getCommandSink().ptr);
});
}
window.__TAURI__?.event.listen("command", (event) => {
const payloadString = JSON.stringify(event.payload);
ServerProtocol.handleCommand(payloadString, commandSink.getCommandSink().ptr);
});

this.updateTauriSettings(props.generalSettings);

this.updateBadge();

Expand Down Expand Up @@ -658,13 +658,20 @@ export class LiveSplit extends React.Component<Props, State> {

this.state.hotkeySystem.setConfig(menu.config);
this.setState({ generalSettings });
this.updateTauriSettings(generalSettings);
} else {
menu.config[Symbol.dispose]();
}

this.openTimerView();
}

private updateTauriSettings(generalSettings: GeneralSettings) {
window.__TAURI__?.tauri.invoke("settings_changed", {
alwaysOnTop: generalSettings.alwaysOnTop,
});
}

public onResize(width: number, height: number) {
this.setState(
{
Expand Down
18 changes: 17 additions & 1 deletion src/ui/MainSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export interface GeneralSettings {
saveOnReset: boolean,
speedrunComIntegration: boolean,
splitsIoIntegration: boolean,
serverUrl: string | undefined,
serverUrl?: string,
alwaysOnTop?: boolean,
}

export interface Props {
Expand Down Expand Up @@ -112,6 +113,11 @@ export class MainSettings extends React.Component<Props, State> {
Bool: this.state.generalSettings.saveOnReset,
},
},
...((window.__TAURI__ != null) ? [{
text: "Always On Top",
tooltip: "Keeps the window always on top of other windows.",
value: { Bool: this.state.generalSettings.alwaysOnTop! },
}] : []),
],
}}
editorUrlCache={this.props.urlCache}
Expand Down Expand Up @@ -162,6 +168,16 @@ export class MainSettings extends React.Component<Props, State> {
});
}
break;
case 4:
if ("Bool" in value) {
this.setState({
generalSettings: {
...this.state.generalSettings,
alwaysOnTop: value.Bool,
},
});
}
break;
}
}}
/>
Expand Down

0 comments on commit 20e73d6

Please sign in to comment.