|
| 1 | +import { |
| 2 | + QAction, |
| 3 | + QIcon, |
| 4 | + QActionSignals, |
| 5 | + Component, |
| 6 | + QFont, |
| 7 | + QShortcut, |
| 8 | + QKeySequence, |
| 9 | + ShortcutContext, |
| 10 | +} from "@nodegui/nodegui"; |
| 11 | +import { RNComponent, RNProps } from "../config"; |
| 12 | +import { throwUnsupported } from "../../utils/helpers"; |
| 13 | + |
| 14 | +export interface ActionProps extends RNProps { |
| 15 | + /** |
| 16 | + * Sets whether the action is a checkable action. [QAction: setCheckable](https://docs.nodegui.org/docs/api/generated/classes/qaction#setcheckable) |
| 17 | + */ |
| 18 | + checkable?: boolean; |
| 19 | + |
| 20 | + /** |
| 21 | + * Sets whether the action is checked. [QAction: setChecked](https://docs.nodegui.org/docs/api/generated/classes/qaction#setchecked) |
| 22 | + */ |
| 23 | + checked?: boolean; |
| 24 | + |
| 25 | + /** |
| 26 | + * Sets whether the action is enabled. [QAction: setEnabled](https://docs.nodegui.org/docs/api/generated/classes/qaction#setenabled) |
| 27 | + */ |
| 28 | + enabled?: boolean; |
| 29 | + |
| 30 | + /** |
| 31 | + * Sets a font for the action. [QAction: setFont](https://docs.nodegui.org/docs/api/generated/classes/qaction#setfont) |
| 32 | + */ |
| 33 | + font?: QFont; |
| 34 | + |
| 35 | + /** |
| 36 | + * Sets an icon for the action. [QSystemTrayIcon: setIcon](https://docs.nodegui.org/docs/api/generated/classes/qsystemtrayicon#seticon) |
| 37 | + */ |
| 38 | + icon?: QIcon; |
| 39 | + |
| 40 | + /** |
| 41 | + * Sets the object name (id) of the widget in Qt. Object name can be analogous to id of an element in the web world. Using the objectName of the widget one can reference it in the Qt's stylesheet much like what we do with id in the web world. [QWidget: setObjectName](https://docs.nodegui.org/docs/api/NodeWidget#widgetsetobjectnameobjectname) |
| 42 | + */ |
| 43 | + id?: string; |
| 44 | + |
| 45 | + /** |
| 46 | + * Prop to set the event listener map. See [Handlong Events](/docs/guides/handle-events) |
| 47 | + */ |
| 48 | + on?: Partial<QActionSignals>; |
| 49 | + |
| 50 | + /** |
| 51 | + * Sets whether this action will be considered a separator. [QAction: setSeparator](https://docs.nodegui.org/docs/api/generated/classes/qaction#setseparator) |
| 52 | + */ |
| 53 | + separator?: boolean; |
| 54 | + |
| 55 | + /** |
| 56 | + * Sets the action's primary shortcut key. [QAction: setShortcut](https://docs.nodegui.org/docs/api/generated/classes/qaction#setshortcut) |
| 57 | + */ |
| 58 | + shortcut?: QKeySequence; |
| 59 | + |
| 60 | + /** |
| 61 | + * Sets the context for action's shortcut. [QAction: setShortcutContext](https://docs.nodegui.org/docs/api/generated/classes/qaction#setshortcutcontext) |
| 62 | + */ |
| 63 | + shortcutContext?: ShortcutContext; |
| 64 | + |
| 65 | + /** |
| 66 | + * Sets descriptive text. [QAction: setText](https://docs.nodegui.org/docs/api/generated/classes/qaction#settext) |
| 67 | + */ |
| 68 | + text?: string; |
| 69 | +} |
| 70 | + |
| 71 | +const setActionProps = ( |
| 72 | + widget: RNAction, |
| 73 | + newProps: ActionProps, |
| 74 | + oldProps: ActionProps |
| 75 | +) => { |
| 76 | + const setter: ActionProps = { |
| 77 | + set checkable(isCheckable: boolean) { |
| 78 | + widget.setCheckable(isCheckable); |
| 79 | + }, |
| 80 | + set checked(isChecked: boolean) { |
| 81 | + widget.setChecked(isChecked); |
| 82 | + }, |
| 83 | + set enabled(isEnabled: boolean) { |
| 84 | + widget.setEnabled(isEnabled); |
| 85 | + }, |
| 86 | + set font(font: QFont) { |
| 87 | + widget.setFont(font); |
| 88 | + }, |
| 89 | + set icon(icon: QIcon) { |
| 90 | + widget.setIcon(icon); |
| 91 | + }, |
| 92 | + set id(id: string) { |
| 93 | + widget.setObjectName(id); |
| 94 | + }, |
| 95 | + set on(listenerMap: Partial<QActionSignals>) { |
| 96 | + const listenerMapLatest: any = Object.assign({}, listenerMap); |
| 97 | + const oldListenerMap = Object.assign({}, oldProps.on); |
| 98 | + Object.entries(oldListenerMap).forEach(([eventType, oldEvtListener]) => { |
| 99 | + const newEvtListener = listenerMapLatest[eventType]; |
| 100 | + if (oldEvtListener !== newEvtListener) { |
| 101 | + widget.removeEventListener(eventType as any, oldEvtListener); |
| 102 | + } else { |
| 103 | + delete listenerMapLatest[eventType]; |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + Object.entries(listenerMapLatest).forEach( |
| 108 | + ([eventType, newEvtListener]) => { |
| 109 | + widget.addEventListener(eventType as any, newEvtListener); |
| 110 | + } |
| 111 | + ); |
| 112 | + }, |
| 113 | + set separator(isSeparator: boolean) { |
| 114 | + widget.setSeparator(isSeparator); |
| 115 | + }, |
| 116 | + set shortcut(shortcut: QKeySequence) { |
| 117 | + widget.setShortcut(shortcut); |
| 118 | + }, |
| 119 | + set shortcutContext(shortcutContext: ShortcutContext) { |
| 120 | + widget.setShortcutContext(shortcutContext); |
| 121 | + }, |
| 122 | + set text(text: string) { |
| 123 | + widget.setText(text); |
| 124 | + }, |
| 125 | + }; |
| 126 | + Object.assign(setter, newProps); |
| 127 | +}; |
| 128 | + |
| 129 | +export class RNAction extends QAction implements RNComponent { |
| 130 | + setProps(newProps: ActionProps, oldProps: ActionProps): void { |
| 131 | + setActionProps(this, newProps, oldProps); |
| 132 | + } |
| 133 | + appendInitialChild(child: Component) { |
| 134 | + throwUnsupported(this); |
| 135 | + } |
| 136 | + appendChild(child: Component): void { |
| 137 | + throwUnsupported(this); |
| 138 | + } |
| 139 | + insertBefore(child: Component, beforeChild: Component): void { |
| 140 | + throwUnsupported(this); |
| 141 | + } |
| 142 | + removeChild(child: Component): void { |
| 143 | + throwUnsupported(this); |
| 144 | + } |
| 145 | + static tagName = "action"; |
| 146 | +} |
0 commit comments