-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mobile): add a mobile connector from frontend
- Loading branch information
Showing
4 changed files
with
142 additions
and
11 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
113 changes: 113 additions & 0 deletions
113
app/renderer/src/contexts/connectors/TauriMobileConnector.tsx
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,113 @@ | ||
import React, { useCallback, useContext, useEffect } from "react"; | ||
import { ConnnectorContext } from "../ConnnectorContext"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import { AppStateTypes, SettingTypes } from "../../store"; | ||
import { CounterContext } from "../CounterContext"; | ||
import { | ||
CHECK_FOR_UPDATES, | ||
SET_ALWAYS_ON_TOP, | ||
SET_CLOSE, | ||
SET_COMPACT_MODE, | ||
SET_FULLSCREEN_BREAK, | ||
SET_MINIMIZE, | ||
SET_NATIVE_TITLEBAR, | ||
SET_SHOW, | ||
SET_UI_THEME, | ||
TRAY_ICON_UPDATE, | ||
UPDATE_AVAILABLE, | ||
} from "@pomatez/shareables"; | ||
import { encodeSvg } from "../../utils"; | ||
import { TraySVG } from "../../components"; | ||
import { enable, disable } from "@tauri-apps/plugin-autostart"; | ||
import { invoke } from "@tauri-apps/api/primitives"; | ||
import { listen } from "@tauri-apps/api/event"; | ||
import { open } from "@tauri-apps/plugin-shell"; | ||
import { setUpdateBody, setUpdateVersion } from "../../store/update"; | ||
|
||
export const TauriMobileInvokeConnector = { | ||
send: (event: string, ...payload: any) => { | ||
invoke(event.toLowerCase(), ...payload).catch((err) => | ||
console.error(err) | ||
); | ||
}, | ||
}; | ||
|
||
export const TauriMobileConnectorProvider: React.FC = ({ | ||
children, | ||
}) => { | ||
const dispatch = useDispatch(); | ||
|
||
/** | ||
* Need to test keyboard input on mobile before removing this. | ||
*/ | ||
useEffect(() => { | ||
function handleKeyDown(event: KeyboardEvent) { | ||
if ( | ||
(event.ctrlKey && (event.key === "r" || event.key === "R")) || | ||
event.key === "F5" | ||
) { | ||
event.preventDefault(); | ||
} | ||
} | ||
|
||
window.addEventListener("keydown", handleKeyDown); | ||
return () => { | ||
window.removeEventListener("keydown", handleKeyDown); | ||
}; | ||
}, []); | ||
|
||
/** | ||
* Apparently you can just use _blank as the target though it definitely isn't working on windows. | ||
* | ||
* This is a workaround :) | ||
*/ | ||
useEffect(() => { | ||
function urlClickHandler(e: MouseEvent) { | ||
const target = e.target as HTMLAnchorElement; | ||
if ( | ||
target && | ||
target.tagName === "A" && | ||
target.href.startsWith("http") && | ||
target.target === "_blank" | ||
) { | ||
e.preventDefault(); | ||
open(target.href); // Use Tauri's shell module to open external links | ||
} | ||
} | ||
window.addEventListener("click", urlClickHandler); | ||
return () => { | ||
window.removeEventListener("click", urlClickHandler); | ||
}; | ||
}, []); | ||
|
||
/** | ||
* Not needed as tauri already opens these externally. | ||
*/ | ||
const openExternalCallback = useCallback(() => {}, []); | ||
|
||
useEffect(() => { | ||
const unlisten = listen<{ body: string; version: string }>( | ||
UPDATE_AVAILABLE, | ||
(updateInfo) => { | ||
console.log("Update Info", updateInfo.payload); | ||
dispatch(setUpdateVersion(updateInfo?.payload?.version)); | ||
dispatch(setUpdateBody(updateInfo?.payload?.body)); | ||
} | ||
); | ||
return () => { | ||
unlisten.then((f) => f()); | ||
}; | ||
}, [dispatch]); | ||
|
||
return ( | ||
<ConnnectorContext.Provider | ||
value={{ | ||
onMinimizeCallback: () => {}, | ||
onExitCallback: () => {}, | ||
openExternalCallback, | ||
}} | ||
> | ||
{children} | ||
</ConnnectorContext.Provider> | ||
); | ||
}; |
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