Skip to content

Commit f38fd1c

Browse files
committed
feat: flatten the tray menu a bit
This PR flattens the list of profiles into the top-level menu, and the list of runs for a profile into that profile's menu.
1 parent 0a5fe4d commit f38fd1c

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

plugins/plugin-codeflare/src/tray/menus/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default async function buildContextMenu(
3333
const contextMenu = Menu.buildFromTemplate([
3434
{ label: `Open CodeFlare`, click: () => createWindow([]) },
3535
{ type: "separator" },
36-
await profilesMenu(createWindow, updateFn),
36+
...(await profilesMenu(createWindow, updateFn)),
3737
{ type: "separator" },
3838
{ label: `Version ${version}`, enabled: false },
3939
{ label: `About CodeFlare`, click: () => import("open").then((_) => _.default(homepage)) },

plugins/plugin-codeflare/src/tray/menus/profiles.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { CreateWindowFunction } from "@kui-shell/core"
2121
import UpdateFunction from "../update"
2222
import windowOptions from "../window"
2323
import { profileIcon, bootIcon, shutDownIcon } from "../icons"
24-
import { RUNS_ERROR, submenuForRuns, readRunsDir } from "./runs"
24+
import submenuForRuns from "./runs"
2525

2626
import ProfileStatusWatcher from "../watchers/profile/status"
2727

@@ -44,31 +44,27 @@ async function shutdown(profile: string, createWindow: CreateWindowFunction) {
4444
const watchers: Record<string, ProfileStatusWatcher> = {}
4545

4646
/** @return a menu for the given profile */
47-
function submenuForOneProfile(
47+
async function submenuForOneProfile(
4848
state: Choices.ChoiceState,
4949
createWindow: CreateWindowFunction,
50-
runs: string[],
5150
updateFunction: UpdateFunction
52-
): MenuItemConstructorOptions {
53-
const isRunsSubMenu =
54-
runs.length && runs[0] !== RUNS_ERROR
55-
? { label: "Runs", submenu: submenuForRuns(createWindow, runs) }
56-
: { label: RUNS_ERROR }
51+
): Promise<MenuItemConstructorOptions> {
5752
if (!watchers[state.profile.name]) {
5853
watchers[state.profile.name] = new ProfileStatusWatcher(state.profile.name, updateFunction)
5954
}
6055
const watcher = watchers[state.profile.name]
6156

6257
return {
6358
label: state.profile.name,
59+
icon: profileIcon,
6460
submenu: [
6561
watcher.head,
6662
watcher.workers,
6763
{ type: "separator" },
6864
{ label: "Boot", icon: bootIcon, click: () => boot(state.profile.name, createWindow) },
6965
{ label: "Shutdown", icon: shutDownIcon, click: () => shutdown(state.profile.name, createWindow) },
7066
{ type: "separator" },
71-
isRunsSubMenu,
67+
...(await submenuForRuns(createWindow)),
7268
],
7369
}
7470
}
@@ -77,13 +73,14 @@ function submenuForOneProfile(
7773
export default async function profilesMenu(
7874
createWindow: CreateWindowFunction,
7975
updateFn: UpdateFunction
80-
): Promise<MenuItemConstructorOptions> {
76+
): Promise<MenuItemConstructorOptions[]> {
8177
const profiles = await Profiles.list({})
82-
const runs = await readRunsDir()
8378

84-
return {
85-
label: "Profiles",
86-
icon: profileIcon,
87-
submenu: profiles.map((_) => submenuForOneProfile(_, createWindow, runs, updateFn)),
88-
}
79+
return [
80+
{
81+
label: "Profiles",
82+
enabled: false,
83+
},
84+
...(await Promise.all(profiles.map((_) => submenuForOneProfile(_, createWindow, updateFn)))),
85+
]
8986
}

plugins/plugin-codeflare/src/tray/menus/runs.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2022 The Kubernetes Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
import { MenuItemConstructorOptions } from "electron"
218
import { Profiles } from "madwizard"
319
import { CreateWindowFunction } from "@kui-shell/core"
@@ -18,7 +34,7 @@ function openRun(runId: string, createWindow: CreateWindowFunction) {
1834
}
1935

2036
/** @return files of the directory of job runs for a given profile */
21-
export async function readRunsDir(): Promise<string[]> {
37+
async function readRunsDir(): Promise<string[]> {
2238
try {
2339
const runsDir = Profiles.guidebookJobDataPath({})
2440
return await readdir(runsDir)
@@ -28,6 +44,17 @@ export async function readRunsDir(): Promise<string[]> {
2844
}
2945

3046
/** @return a menu for all runs of a profile */
31-
export function submenuForRuns(createWindow: CreateWindowFunction, runs: string[]): MenuItemConstructorOptions[] {
47+
export function runMenuItems(createWindow: CreateWindowFunction, runs: string[]): MenuItemConstructorOptions[] {
3248
return runs.map((run) => ({ label: run, click: () => openRun(run, createWindow) }))
3349
}
50+
51+
export default async function submenuForRuns(
52+
createWindow: CreateWindowFunction
53+
): Promise<MenuItemConstructorOptions[]> {
54+
const runs = await readRunsDir()
55+
56+
return [
57+
{ label: "Recent Runs", enabled: false },
58+
...(runs.length && runs[0] !== RUNS_ERROR ? runMenuItems(createWindow, runs) : [{ label: RUNS_ERROR }]),
59+
]
60+
}

0 commit comments

Comments
 (0)