Skip to content

Commit 35b221a

Browse files
move all ui related functionality to a separate class
Signed-off-by: Nikita Skrynnik <[email protected]>
1 parent c252c94 commit 35b221a

File tree

7 files changed

+61
-47
lines changed

7 files changed

+61
-47
lines changed

src/components/browser/Browser.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ class InputHandler {
150150
}
151151

152152
canvas.onfocus = () => {
153-
app.setBrowserFocused(true);
153+
app.ui.setBrowserFocused(true);
154154
connection.page.focus(true);
155155
};
156156
canvas.onblur = () => {
157-
app.setBrowserFocused(false);
157+
app.ui.setBrowserFocused(false);
158158
connection.page.focus(false);
159159
};
160160
}

src/components/browser/BrowserContextMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ContextMenu } from "@kobalte/core/context-menu";
22
import { ArrowLeft, ArrowRight, RotateCcw } from "lucide-solid";
33
import styles from "./BrowserContextMenu.module.scss";
4-
import { AppState } from "../state/state";
4+
import { AppState } from "../../state/state";
55
import { createMemo, JSX, Show } from "solid-js";
66

77
function BrowserContextMenu(props: { app: AppState, children: JSX.Element }) {

src/components/sidebar/Input.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default function Input(props: { app: AppState }) {
99
let activeTabMemo = createMemo(() => props.app.getActiveTab());
1010

1111
onMount(() => {
12-
props.app.setFocusUrlCallback(() => ref.focus());
12+
props.app.ui.setFocusUrlCallback(() => ref.focus());
1313
})
1414

1515
createEffect(() => {

src/components/sidebar/Layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default function Sidebar(props: { app: AppState }) {
1717
const onNewTabClick = () => setShowInput(true);
1818

1919
onMount(() => {
20-
props.app.setShowNewTabInputCallback(() => setShowInput(true));
20+
props.app.ui.setShowNewTabInputCallback(() => setShowInput(true));
2121
});
2222

2323
return (

src/state/shortcuts.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class Shortcuts {
2727
action.execute();
2828
}
2929

30-
if (action.ctx == "webpage" && app.browserFocused()) {
30+
if (action.ctx == "webpage" && app.ui.browserFocused()) {
3131
e.preventDefault();
3232
action.execute();
3333
}
@@ -50,7 +50,7 @@ export class Shortcuts {
5050
this.shortcuts.set("ctrl+shift+tab", "previousTab");
5151
this.shortcuts.set("ctrl+shift+t", "restoreSession");
5252

53-
this.actions.set("showInput", { ctx: "global", execute: () => app.showNewTabInput() });
53+
this.actions.set("showInput", { ctx: "global", execute: () => app.ui.showNewTabInput() });
5454
this.actions.set("closeTab", { ctx: "global", execute: () => app.getActiveTab()?.close() });
5555
this.actions.set("reload", { ctx: "webpage", execute: () => app.getActiveTab()?.reload() });
5656
this.actions.set("selectAll", { ctx: "webpage", execute: () => app.getActiveTab()?.selectAll() });
@@ -59,7 +59,7 @@ export class Shortcuts {
5959
this.actions.set("cut", { ctx: "webpage", execute: () => app.getActiveTab()?.cut() });
6060
this.actions.set("undo", { ctx: "webpage", execute: () => app.getActiveTab()?.undo() });
6161
this.actions.set("redo", { ctx: "webpage", execute: () => app.getActiveTab()?.redo() });
62-
this.actions.set("focusOnAddressBar", { ctx: "global", execute: () => app.focusUrl() });
62+
this.actions.set("focusOnAddressBar", { ctx: "global", execute: () => app.ui.focusUrl() });
6363
this.actions.set("nextTab", { ctx: "global", execute: () => app.shiftTab(true) });
6464
this.actions.set("previousTab", { ctx: "global", execute: () => app.shiftTab(false) });
6565
this.actions.set("restoreSession", { ctx: "global", execute: () => app.restore() });

src/state/state.ts

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { createStore, SetStoreFunction } from "solid-js/store";
33
import { ProfileManager } from "./profiles";
44
import { isURL, isFQDN } from "validator";
55
import { invoke } from "@tauri-apps/api/core";
6-
import { Accessor, createSignal, Setter } from "solid-js";
6+
import { Setter } from "solid-js";
77
import { Shortcuts } from "./shortcuts";
88
import { DownloadProgress, FileDialog } from "cef-client/dist/event_stream";
99
import { Downloads } from "./downloads";
10+
import { UIState } from "./ui";
1011
import { open } from '@tauri-apps/plugin-dialog';
1112

1213
type TabId = number;
@@ -49,26 +50,20 @@ export class AppState {
4950
downloads: Downloads;
5051
shortcuts: Shortcuts;
5152
profiles: ProfileManager | undefined;
53+
ui: UIState;
5254

5355
tabs: TabState[];
5456
setTabs: SetStoreFunction<TabState[]>;
5557
connections: Map<TabId, TabConnection> = new Map();
5658

57-
browserFocused: Accessor<boolean>;
58-
setBrowserFocused: Setter<boolean>;
59-
60-
private focusUrlCallback: (() => void) | null = null;
61-
private showNewTabInputCallback: (() => void) | null = null;
62-
6359
constructor(client: Browser, profiles?: ProfileManager) {
6460
this.client = client;
6561

6662
this.downloads = new Downloads();
6763
this.shortcuts = new Shortcuts(this);
6864
this.profiles = profiles;
65+
this.ui = new UIState();
6966
[this.tabs, this.setTabs] = createStore<TabState[]>([]);
70-
71-
[this.browserFocused, this.setBrowserFocused] = createSignal(false);
7267
}
7368

7469
async setClient(client: Browser) {
@@ -97,36 +92,7 @@ export class AppState {
9792
let tab = await this.client.openTab({ url })!;
9893
this.addTab(tab);
9994
this.setActiveTab(tab.id);
100-
this.focusUrl();
101-
}
102-
103-
setFocusUrlCallback(callback: () => void) {
104-
this.focusUrlCallback = callback;
105-
}
106-
107-
setShowNewTabInputCallback(callback: () => void) {
108-
this.showNewTabInputCallback = callback;
109-
}
110-
111-
focusUrl() {
112-
if (this.focusUrlCallback) this.focusUrlCallback();
113-
}
114-
115-
showNewTabInput() {
116-
if (this.showNewTabInputCallback) {
117-
this.showNewTabInputCallback();
118-
} else {
119-
console.warn("showNewTabInputCallback is not set");
120-
}
121-
}
122-
123-
isBrowserFocused(): boolean {
124-
return this.browserFocused();
125-
}
126-
127-
getStackTrace(): string {
128-
const error = new Error();
129-
return error.stack ?? 'No stack trace available';
95+
this.ui.focusUrl();
13096
}
13197

13298
getActiveTab(): TabState | undefined {

src/state/ui.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Accessor, createSignal, Setter } from "solid-js";
2+
3+
export interface UICallbacks {
4+
focusUrl: (() => void) | null;
5+
showNewTabInput: (() => void) | null;
6+
}
7+
8+
export class UIState {
9+
browserFocused: Accessor<boolean>;
10+
setBrowserFocused: Setter<boolean>;
11+
12+
private callbacks: UICallbacks;
13+
14+
constructor() {
15+
[this.browserFocused, this.setBrowserFocused] = createSignal(false);
16+
17+
this.callbacks = {
18+
focusUrl: null,
19+
showNewTabInput: null
20+
};
21+
}
22+
23+
setFocusUrlCallback(callback: () => void) {
24+
this.callbacks.focusUrl = callback;
25+
}
26+
27+
setShowNewTabInputCallback(callback: () => void) {
28+
this.callbacks.showNewTabInput = callback;
29+
}
30+
31+
focusUrl() {
32+
if (this.callbacks.focusUrl) {
33+
this.callbacks.focusUrl();
34+
}
35+
}
36+
37+
showNewTabInput() {
38+
if (this.callbacks.showNewTabInput) {
39+
this.callbacks.showNewTabInput();
40+
} else {
41+
console.warn("showNewTabInputCallback is not set");
42+
}
43+
}
44+
45+
isBrowserFocused(): boolean {
46+
return this.browserFocused();
47+
}
48+
}

0 commit comments

Comments
 (0)