Skip to content

Commit

Permalink
Add: 最近開いたプロジェクト一覧を追加 (#1264)
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenc-nanashi authored Apr 8, 2023
1 parent c11f551 commit 64e0447
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 3 deletions.
57 changes: 56 additions & 1 deletion src/components/MenuBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,24 @@ export type MenuItemBase<T extends string> = {
export type MenuItemSeparator = MenuItemBase<"separator">;
export type MenuItemRoot = MenuItemBase<"root"> & {
onClick: () => void;
onClick?: () => void;
subMenu: MenuItemData[];
icon?: string;
disabled?: boolean;
disableWhenUiLocked: boolean;
};
export type MenuItemButton = MenuItemBase<"button"> & {
onClick: () => void;
icon?: string;
disabled?: boolean;
disableWhenUiLocked: boolean;
};
export type MenuItemCheckbox = MenuItemBase<"checkbox"> & {
checked: ComputedRef<boolean>;
onClick: () => void;
disabled?: boolean;
disableWhenUiLocked: boolean;
};
Expand Down Expand Up @@ -300,6 +303,12 @@ const menudata = ref<MenuItemData[]>([
},
disableWhenUiLocked: true,
},
{
type: "root",
label: "最近使ったプロジェクト",
disableWhenUiLocked: true,
subMenu: [],
},
],
},
{
Expand Down Expand Up @@ -523,6 +532,52 @@ if (store.state.isMultiEngineOffMode) {
});
}
// 「最近開いたプロジェクト」の更新
async function updateRecentProjects() {
const projectsMenu = menudata.value.find(
(x) => x.type === "root" && x.label === "ファイル"
) as MenuItemRoot;
const recentProjectsMenu = projectsMenu.subMenu.find(
(x) => x.type === "root" && x.label === "最近使ったプロジェクト"
) as MenuItemRoot;
const recentlyUsedProjects = await store.dispatch(
"GET_RECENTLY_USED_PROJECTS"
);
recentProjectsMenu.subMenu =
recentlyUsedProjects.length === 0
? [
{
type: "button",
label: "最近使ったプロジェクトはありません",
onClick: () => {
// 何もしない
},
disabled: true,
disableWhenUiLocked: false,
},
]
: recentlyUsedProjects.map(
(projectFilePath) =>
({
type: "button",
label: projectFilePath,
onClick: () => {
store.dispatch("LOAD_PROJECT_FILE", {
filePath: projectFilePath,
});
},
disableWhenUiLocked: false,
} as MenuItemData)
);
}
const projectFilePath = computed(() => store.state.projectFilePath);
watch(projectFilePath, updateRecentProjects, {
immediate: true,
});
watch(uiLocked, () => {
// UIのロックが解除された時に再びメニューが開かれてしまうのを防ぐ
if (uiLocked.value) {
Expand Down
4 changes: 2 additions & 2 deletions src/components/MenuButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
:disable="disable"
@click="
(menudata.type === 'button' || menudata.type === 'root') &&
menudata.onClick()
menudata.onClick?.()
"
>
{{ menudata.label }}
<q-menu
v-if="menudata.subMenu"
v-if="'subMenu' in menudata"
transition-show="none"
transition-hide="none"
:fit="true"
Expand Down
6 changes: 6 additions & 0 deletions src/components/MenuItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
v-else-if="menudata.type === 'root'"
clickable
dense
:disable="menudata.disabled"
:class="selected && 'active-menu'"
@click="menudata.onClick"
>
<q-item-section side class="q-py-2" v-if="menudata.icon">
<img :src="menudata.icon" class="engine-icon" />
</q-item-section>

<q-item-section>{{ menudata.label }}</q-item-section>
<q-item-section side v-if="getMenuBarHotkey(menudata.label)">
{{ getMenuBarHotkey(menudata.label) }}
</q-item-section>

<q-item-section side>
<q-icon name="keyboard_arrow_right" />
Expand Down Expand Up @@ -40,6 +45,7 @@
v-ripple
v-close-popup
class="bg-background"
:disable="menudata.disabled"
@click="menudata.onClick"
>
<q-item-section v-if="menudata.type === 'checkbox'" side class="q-pr-sm">
Expand Down
6 changes: 6 additions & 0 deletions src/store/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ export const projectStore = createPartialStore<ProjectStoreTypes>({
const projectFileErrorMsg = `VOICEVOX Project file "${filePath}" is a invalid file.`;

try {
await context.dispatch("APPEND_RECENTLY_USED_PROJECT", {
filePath,
});
const buf = await window.electron.readFile({ filePath });
const text = new TextDecoder("utf-8").decode(buf).trim();
const projectData = JSON.parse(text);
Expand Down Expand Up @@ -407,6 +410,9 @@ export const projectStore = createPartialStore<ProjectStoreTypes>({
});
}

await context.dispatch("APPEND_RECENTLY_USED_PROJECT", {
filePath,
});
const appInfos = await window.electron.getAppInfos();
const { audioItems, audioKeys } = context.state;
const projectData: ProjectType = {
Expand Down
20 changes: 20 additions & 0 deletions src/store/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,26 @@ export const settingStore = createPartialStore<SettingStoreTypes>({
}
),
},

GET_RECENTLY_USED_PROJECTS: {
async action() {
return await window.electron.getSetting("recentlyUsedProjects");
},
},

APPEND_RECENTLY_USED_PROJECT: {
async action({ dispatch }, { filePath }) {
const recentlyUsedProjects = await dispatch("GET_RECENTLY_USED_PROJECTS");
const newRecentlyUsedProjects = [
filePath,
...recentlyUsedProjects.filter((value) => value != filePath),
].slice(0, 10);
await window.electron.setSetting(
"recentlyUsedProjects",
newRecentlyUsedProjects
);
},
},
});

export const setHotkeyFunctions = (
Expand Down
8 changes: 8 additions & 0 deletions src/store/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,14 @@ export type SettingStoreTypes = {
CHANGE_USE_GPU: {
action(payload: { useGpu: boolean; engineId: EngineId }): Promise<void>;
};

GET_RECENTLY_USED_PROJECTS: {
action(): Promise<string[]>;
};

APPEND_RECENTLY_USED_PROJECT: {
action(payload: { filePath: string }): Promise<void>;
};
};

/*
Expand Down
1 change: 1 addition & 0 deletions src/type/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ export const electronStoreSchema = z
.passthrough()
.default({}),
registeredEngineDirs: z.string().array().default([]),
recentlyUsedProjects: z.string().array().default([]),
})
.passthrough();
export type ElectronStoreType = z.infer<typeof electronStoreSchema>;
Expand Down

0 comments on commit 64e0447

Please sign in to comment.