Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape Keybinding #282

Merged
merged 2 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/renderer/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import PromptContainer from "@renderer/components/prompts/PromptContainer.vue";
import { playRandomMusic } from "@renderer/utils/play-random-music";
import { settingsStore } from "./store/settings.store";
import { infosStore } from "@renderer/store/infos.store";
import { useGlobalKeybindings } from "@renderer/composables/useGlobalKeybindings";

const router = useRouter();
const videoVisible = toRef(!toValue(settingsStore.skipIntro));
Expand All @@ -83,6 +84,8 @@ const exitOpen = ref(false);
provide("settingsOpen", settingsOpen);
provide("exitOpen", exitOpen);

useGlobalKeybindings({ exitOpen });

const toggleMessages: Ref<((open?: boolean, userId?: number) => void) | undefined> = ref();
provide("toggleMessages", toggleMessages);

Expand Down
13 changes: 13 additions & 0 deletions src/renderer/components/common/Modal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { nextTick, onMounted, Ref, ref, toRef, watch } from "vue";

import Panel from "@renderer/components/common/Panel.vue";
import { audioApi } from "@renderer/audio/audio";
import { onKeyDown } from "@vueuse/core";

export type PanelProps = InstanceType<typeof Panel>["$props"];
export interface ModalProps extends /* @vue-ignore */ PanelProps {
Expand Down Expand Up @@ -81,6 +82,18 @@ watch(isOpen, (open) => {
}
});

onKeyDown(
"Escape",
(e) => {
if (isOpen.value) {
e.preventDefault();
e.stopPropagation();
close();
}
},
{ target: document }
);

async function onSubmit() {
const data: Record<string, unknown> = {};

Expand Down
30 changes: 30 additions & 0 deletions src/renderer/composables/useGlobalKeybindings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Ref } from "vue";
import { useRouter } from "vue-router";
import { onKeyDown } from "@vueuse/core";
import { settingsStore } from "@renderer/store/settings.store";

type KeybindingProps = { exitOpen: Ref<boolean, boolean> };

export function useGlobalKeybindings({ exitOpen }: KeybindingProps) {
const router = useRouter();

onKeyDown("F11", (e) => {
e.preventDefault();
settingsStore.fullscreen = !settingsStore.fullscreen;
});

onKeyDown("Escape", (e) => {
e.preventDefault();
if (router.currentRoute.value.path.startsWith("/login") || router.currentRoute.value.path.startsWith("/home")) {
exitOpen.value = !exitOpen.value;
} else {
const routeSegments = router.currentRoute.value.path.split("/");
// "/primary/secondary" => ["", "primary", "secondary", ...]
if (routeSegments.length <= 3) {
router.push("/home");
} else {
router.back();
}
}
});
}
40 changes: 8 additions & 32 deletions src/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import "@renderer/styles/styles.scss";

import PrimeVue from "primevue/config";
import Tooltip from "primevue/tooltip";
import type { TransitionProps } from "vue";
import { createApp } from "vue";
import { createI18n } from "vue-i18n";
import { localeFilePaths } from "@renderer/assets/assetFiles";
Expand All @@ -15,50 +14,27 @@ import { clickAwayDirective } from "@renderer/utils/click-away-directive";
import { elementInViewDirective } from "@renderer/utils/element-in-view-directive";
import { audioApi } from "@renderer/audio/audio";
import { router } from "@renderer/router";
import { settingsStore } from "@renderer/store/settings.store";
import { initPreMountStores } from "@renderer/store/stores";

declare module "vue-router" {
interface RouteMeta {
title?: string;
order?: number;
availableOffline?: boolean;
hide?: boolean;
empty?: boolean;
blurBg?: boolean;
transition?: TransitionProps;
overflowY?: "scroll" | "hidden";
devOnly?: boolean;
redirect?: string;
}
}

(async () => {
await setupVue();
window.addEventListener("keydown", (event) => {
if (event.code === "F11") {
event.preventDefault();
settingsStore.fullscreen = !settingsStore.fullscreen;
}
});
})();
setupVue();

async function setupVue() {
const app = createApp(App);

// Plugins
app.use(router);
app.use(PrimeVue, {
ripple: true,
});
app.use(PrimeVue, { ripple: true });
app.use(await setupI18n());

// Directives
app.directive("click-away", clickAwayDirective);
app.directive("in-view", elementInViewDirective);
app.directive("tooltip", Tooltip);
if (process.env.NODE_ENV !== "production") {
app.config.globalProperties.window = window;
}

// Init stores before mounting app
await initPreMountStores();
await audioApi.init();

app.mount("#app");
}

Expand Down
18 changes: 17 additions & 1 deletion src/renderer/interface.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AccountApi, DownloadsApi, EngineApi, GameApi, InfoApi, MainWindowApi, MapsApi, MiscApi, ReplaysApi, SettingsApi, ShellApi } from "@preload/preload";
import type { AccountApi, DownloadsApi, EngineApi, GameApi, InfoApi, MainWindowApi, MapsApi, MiscApi, ReplaysApi, SettingsApi, ShellApi } from "@preload/preload";
import type { TransitionProps } from "vue";

declare global {
interface Window {
Expand All @@ -15,3 +16,18 @@ declare global {
misc: MiscApi;
}
}

declare module "vue-router" {
interface RouteMeta {
title?: string;
order?: number;
availableOffline?: boolean;
hide?: boolean;
empty?: boolean;
blurBg?: boolean;
transition?: TransitionProps;
overflowY?: "scroll" | "hidden";
devOnly?: boolean;
redirect?: string;
}
}