Skip to content

Commit

Permalink
Support selective hiding of custom buttons (#123)
Browse files Browse the repository at this point in the history
- add an optional isVisible() function to the custom button args

Signed-off-by: Christian W. Damus <[email protected]>
  • Loading branch information
cdamus authored Sep 12, 2024
1 parent 8b257e2 commit ddd7bfb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
26 changes: 25 additions & 1 deletion ui/src/frontend/button_registry.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
import { Registry } from '../common/registry';
// Copyright (C) 2024 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {Registry} from '../common/registry';

export interface CustomButtonArgs {
title: string;
icon: string;
callback: () => {};
/**
* An optional function determining whether the button should be shown.
* If absent, the button will always be shown.
*/
isVisible?: () => boolean;
}

export class CustomButton {
title: string;
icon: string;
callback: () => {};
isVisible?: () => boolean;
kind: string;

constructor(args: CustomButtonArgs) {
this.title = args.title;
this.icon = args.icon;
this.callback = args.callback;
this.isVisible = args.isVisible;
this.kind = args.title;
}
}

export const customButtonRegistry = Registry.kindRegistry<CustomButton>();
16 changes: 7 additions & 9 deletions ui/src/frontend/notes_panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,25 +120,23 @@ export class NotesPanel extends Panel {

private renderCustomButtons(): m.Vnode<any, any>[] {
const mithrilButtons: m.Vnode<any, any>[] = [];
const values = customButtonRegistry.values();
let result = values.next();
while (!result.done) {
if (!result.value) {
break;
for (const customButton of customButtonRegistry.values()) {
if (customButton.isVisible && !customButton.isVisible()) {
// This button is not to be shown at this time
continue;
}
const savedValue = result.value;

const htmlButton = m(
'button',
{
onclick: (e: Event) => {
e.preventDefault();
savedValue.callback();
customButton.callback();
},
},
m('i.material-icons', {title: result.value.title}, result.value.icon),
m('i.material-icons', {title: customButton.title}, customButton.icon),
);
mithrilButtons.push(htmlButton);
result =values.next();
}
return mithrilButtons;
}
Expand Down

0 comments on commit ddd7bfb

Please sign in to comment.