-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
423 additions
and
275 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
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
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
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,75 @@ | ||
import { ObservableValue } from '@lib/observable-value'; | ||
|
||
export class WindowService { | ||
private _tabRecords = new Set<number>(); | ||
private _templatePath: string = ''; | ||
|
||
private _window: chrome.windows.Window | null = null; | ||
|
||
private _onWindowClosedSubscription$ = new ObservableValue<void>(undefined); | ||
|
||
get window() { | ||
return this._window; | ||
} | ||
|
||
setWindowTemplatePath(path: string) { | ||
this._templatePath = path; | ||
} | ||
|
||
async ensureWindow() { | ||
if (!this._window) { | ||
await this.createWindow(); | ||
} | ||
|
||
return this._window!; | ||
} | ||
|
||
async createWindow() { | ||
const currentWindow = await chrome.windows.getCurrent(); | ||
|
||
this._window = await chrome.windows.create({ | ||
url: this._templatePath, | ||
width: currentWindow.width ?? 1280, | ||
height: currentWindow.height ?? 920, | ||
state: 'normal', | ||
focused: false, | ||
}); | ||
} | ||
|
||
closeWindow() { | ||
if (this._window) { | ||
const windowId = this._window.id; | ||
this._window = null; | ||
|
||
windowId && chrome.windows.remove(windowId); | ||
} | ||
} | ||
|
||
onWindowClosed(callback: () => void) { | ||
return this._onWindowClosedSubscription$.subscribe(callback); | ||
} | ||
|
||
listenMessage() { | ||
chrome.windows.onRemoved.addListener((windowId) => { | ||
if (this._window && this._window.id === windowId) { | ||
this._window = null; | ||
this._onWindowClosedSubscription$.next(); | ||
} | ||
}); | ||
|
||
chrome.tabs.onCreated.addListener((tab) => { | ||
if (tab.id | ||
&& tab.windowId === this._window?.id | ||
&& tab.pendingUrl !== this._templatePath) { | ||
this._tabRecords.add(tab.id); | ||
} | ||
}); | ||
|
||
chrome.tabs.onRemoved.addListener((tabId) => { | ||
this._tabRecords.delete(tabId); | ||
if (this._window && this._tabRecords.size === 0) { | ||
this.closeWindow(); | ||
} | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
export function isFunction(value: any): value is Function { | ||
return typeof value === 'function'; | ||
} | ||
|
||
export function waitFor(ms: number) { | ||
return new Promise<void>((resolve) => { | ||
setTimeout(resolve, ms); | ||
}); | ||
}; |
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
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
Oops, something went wrong.