Skip to content

Commit 1e08450

Browse files
committed
[chrome] implement popstate and make virtual states better
1 parent ae4c229 commit 1e08450

File tree

6 files changed

+44
-33
lines changed

6 files changed

+44
-33
lines changed

packages/chrome/src/Browser.tsx

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,6 @@ import type { ScramjetDownload } from "@mercuryworkshop/scramjet";
2020

2121
export let browser: Browser;
2222

23-
export const config = createState({
24-
theme: {
25-
frame_bg: [231, 238, 245],
26-
toolbar_bg: [211, 218, 255],
27-
toolbar_button_fg: [65, 72, 76],
28-
toolbar_fg: [65, 72, 76],
29-
30-
inactive_tab_bg: [40, 40, 40],
31-
inactive_tab_fg: [95, 92, 96],
32-
active_tab_fg: [65, 72, 76],
33-
34-
button_bg: [231, 238, 0],
35-
36-
ntp_bg: [231, 238, 0],
37-
ntp_fg: [232, 234, 237],
38-
ntp_link_fg: [138, 180, 248],
39-
40-
omnibox_bg: [221, 228, 235],
41-
omnibox_fg: [227, 227, 227],
42-
43-
bookmark_fg: [199, 199, 199],
44-
},
45-
});
46-
4723
export type SerializedBrowser = {
4824
tabs: SerializedTab[];
4925
globalhistory: SerializedHistoryState[];

packages/chrome/src/History.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,18 @@ export class History {
8080
return this.states[this.index];
8181
}
8282

83-
push(url: URL, state: any = null, navigate: boolean = true): HistoryState {
83+
push(
84+
url: URL,
85+
state: any = null,
86+
navigate: boolean = true,
87+
virtual: boolean = false
88+
): HistoryState {
8489
if (this.index + 1 < this.states.length)
8590
// "fork" history tree, creating a new timeline
8691
this.states.splice(this.index, this.states.length - this.index);
8792
const hstate = new HistoryState({ url, state });
93+
if (virtual) hstate.virtual = true;
94+
8895
if (url.href != "puter://newtab") browser.globalhistory.push(hstate);
8996
this.states.push(hstate);
9097
this.index++;
@@ -130,8 +137,10 @@ export class History {
130137
let newstate = this.states[this.index];
131138

132139
if (newstate.virtual) {
133-
sendFrame(this.tab, "history_go", {
134-
delta,
140+
sendFrame(this.tab, "popstate", {
141+
state: newstate.state,
142+
url: newstate.url.href,
143+
title: newstate.title || "",
135144
});
136145
} else if (navigate) {
137146
this.justTriggeredNavigation = true;

packages/chrome/src/IsolatedFrame.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,4 +819,20 @@ const chromemethods: ChromeboundMethods = {
819819
tab.history.push(new URL(url), undefined, false);
820820
}
821821
},
822+
823+
history_go: async (tab, { delta }) => {
824+
if (tab) {
825+
tab.history.go(delta);
826+
}
827+
},
828+
history_pushState: async (tab, { url, title }) => {
829+
if (tab) {
830+
tab.history.push(new URL(url), title, true);
831+
}
832+
},
833+
history_replaceState: async (tab, { url, title }) => {
834+
if (tab) {
835+
tab.history.replace(new URL(url), title);
836+
}
837+
},
822838
};

packages/chrome/src/pages/NewTabPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ NewTabPage.style = css`
113113
}
114114
.inputcontainer {
115115
flex: 1;
116-
max-width: 40em;
116+
max-width: 60em;
117117
box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.1);
118118
background: var(--bg20);
119119
border-radius: var(--radius);

packages/inject/src/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import { sendChrome } from "./ipc";
77
export const client = self[SCRAMJETCLIENT];
88
export const chromeframe = top!;
99

10+
const history_replaceState = History.prototype.replaceState;
11+
1012
export const methods: FrameboundMethods = {
1113
async navigate({ url }) {
1214
window.location.href = url;
1315
},
14-
async history_go({ delta }) {
15-
client.natives.call("History.prototype.go", history, delta);
16+
async popstate({ url, state, title }) {
17+
history_replaceState.call(history, state, title, url);
18+
const popStateEvent = new PopStateEvent("popstate", { state });
19+
window.dispatchEvent(popStateEvent);
1620
},
1721
};
1822

@@ -114,7 +118,7 @@ function setupHistoryEmulation() {
114118
sendChrome("history_pushState", {
115119
state: ctx.args[0],
116120
title: ctx.args[1],
117-
url: ctx.args[2],
121+
url: new URL(ctx.args[2], client.url).href,
118122
});
119123

120124
ctx.return(undefined);
@@ -126,7 +130,7 @@ function setupHistoryEmulation() {
126130
sendChrome("history_replaceState", {
127131
state: ctx.args[0],
128132
title: ctx.args[1],
129-
url: ctx.args[2],
133+
url: new URL(ctx.args[2], client.url).href,
130134
});
131135

132136
ctx.return(undefined);

packages/inject/src/types.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,11 @@ export type Framebound = {
5757
url: string;
5858
},
5959
];
60-
history_go: [{ delta: number }, void];
60+
popstate: [
61+
{
62+
state: any;
63+
url: string;
64+
title: string;
65+
},
66+
];
6167
};

0 commit comments

Comments
 (0)