Add a method to set the escape identifier on the touch bar
This commit is contained in:
parent
3c1a372157
commit
4d6b0fc01b
10 changed files with 66 additions and 1 deletions
|
@ -853,6 +853,10 @@ void Window::RefreshTouchBarItem(const std::string& item_id) {
|
||||||
window_->RefreshTouchBarItem(item_id);
|
window_->RefreshTouchBarItem(item_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Window::SetEscapeTouchBarItem(const mate::PersistentDictionary item) {
|
||||||
|
window_->SetEscapeTouchBarItem(item);
|
||||||
|
}
|
||||||
|
|
||||||
int32_t Window::ID() const {
|
int32_t Window::ID() const {
|
||||||
return weak_map_id();
|
return weak_map_id();
|
||||||
}
|
}
|
||||||
|
@ -975,6 +979,7 @@ void Window::BuildPrototype(v8::Isolate* isolate,
|
||||||
.SetMethod("setVibrancy", &Window::SetVibrancy)
|
.SetMethod("setVibrancy", &Window::SetVibrancy)
|
||||||
.SetMethod("_setTouchBarItems", &Window::SetTouchBar)
|
.SetMethod("_setTouchBarItems", &Window::SetTouchBar)
|
||||||
.SetMethod("_refreshTouchBarItem", &Window::RefreshTouchBarItem)
|
.SetMethod("_refreshTouchBarItem", &Window::RefreshTouchBarItem)
|
||||||
|
.SetMethod("_setEscapeTouchBarItem", &Window::SetEscapeTouchBarItem)
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
.SetMethod("hookWindowMessage", &Window::HookWindowMessage)
|
.SetMethod("hookWindowMessage", &Window::HookWindowMessage)
|
||||||
.SetMethod("isWindowMessageHooked", &Window::IsWindowMessageHooked)
|
.SetMethod("isWindowMessageHooked", &Window::IsWindowMessageHooked)
|
||||||
|
|
|
@ -208,6 +208,7 @@ class Window : public mate::TrackableObject<Window>,
|
||||||
void SetVibrancy(mate::Arguments* args);
|
void SetVibrancy(mate::Arguments* args);
|
||||||
void SetTouchBar(const std::vector<mate::PersistentDictionary>& items);
|
void SetTouchBar(const std::vector<mate::PersistentDictionary>& items);
|
||||||
void RefreshTouchBarItem(const std::string& item_id);
|
void RefreshTouchBarItem(const std::string& item_id);
|
||||||
|
void SetEscapeTouchBarItem(const mate::PersistentDictionary item);
|
||||||
|
|
||||||
v8::Local<v8::Value> WebContents(v8::Isolate* isolate);
|
v8::Local<v8::Value> WebContents(v8::Isolate* isolate);
|
||||||
|
|
||||||
|
|
|
@ -347,6 +347,9 @@ void NativeWindow::SetTouchBar(
|
||||||
void NativeWindow::RefreshTouchBarItem(const std::string& item_id) {
|
void NativeWindow::RefreshTouchBarItem(const std::string& item_id) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NativeWindow::SetEscapeTouchBarItem(const mate::PersistentDictionary item) {
|
||||||
|
}
|
||||||
|
|
||||||
void NativeWindow::FocusOnWebView() {
|
void NativeWindow::FocusOnWebView() {
|
||||||
web_contents()->GetRenderViewHost()->GetWidget()->Focus();
|
web_contents()->GetRenderViewHost()->GetWidget()->Focus();
|
||||||
}
|
}
|
||||||
|
|
|
@ -174,6 +174,7 @@ class NativeWindow : public base::SupportsUserData,
|
||||||
virtual void SetTouchBar(
|
virtual void SetTouchBar(
|
||||||
const std::vector<mate::PersistentDictionary>& items);
|
const std::vector<mate::PersistentDictionary>& items);
|
||||||
virtual void RefreshTouchBarItem(const std::string& item_id);
|
virtual void RefreshTouchBarItem(const std::string& item_id);
|
||||||
|
virtual void SetEscapeTouchBarItem(const mate::PersistentDictionary item);
|
||||||
|
|
||||||
// Webview APIs.
|
// Webview APIs.
|
||||||
virtual void FocusOnWebView();
|
virtual void FocusOnWebView();
|
||||||
|
|
|
@ -103,6 +103,7 @@ class NativeWindowMac : public NativeWindow,
|
||||||
void SetTouchBar(
|
void SetTouchBar(
|
||||||
const std::vector<mate::PersistentDictionary>& items) override;
|
const std::vector<mate::PersistentDictionary>& items) override;
|
||||||
void RefreshTouchBarItem(const std::string& item_id) override;
|
void RefreshTouchBarItem(const std::string& item_id) override;
|
||||||
|
void SetEscapeTouchBarItem(const mate::PersistentDictionary item) override;
|
||||||
|
|
||||||
// content::RenderWidgetHost::InputEventObserver:
|
// content::RenderWidgetHost::InputEventObserver:
|
||||||
void OnInputEvent(const blink::WebInputEvent& event) override;
|
void OnInputEvent(const blink::WebInputEvent& event) override;
|
||||||
|
|
|
@ -410,6 +410,11 @@ enum {
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-(void)setEscapeTouchBarItem:(mate::PersistentDictionary)item {
|
||||||
|
if (self.touchBar && atom_touch_bar_)
|
||||||
|
[atom_touch_bar_ setEscapeTouchBarItem:item forTouchBar:self.touchBar];
|
||||||
|
}
|
||||||
|
|
||||||
// NSWindow overrides.
|
// NSWindow overrides.
|
||||||
|
|
||||||
- (void)swipeWithEvent:(NSEvent *)event {
|
- (void)swipeWithEvent:(NSEvent *)event {
|
||||||
|
@ -1417,6 +1422,10 @@ void NativeWindowMac::RefreshTouchBarItem(const std::string& item_id) {
|
||||||
[window_ refreshTouchBarItem:item_id];
|
[window_ refreshTouchBarItem:item_id];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NativeWindowMac::SetEscapeTouchBarItem(const mate::PersistentDictionary item) {
|
||||||
|
[window_ setEscapeTouchBarItem:item];
|
||||||
|
}
|
||||||
|
|
||||||
void NativeWindowMac::OnInputEvent(const blink::WebInputEvent& event) {
|
void NativeWindowMac::OnInputEvent(const blink::WebInputEvent& event) {
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case blink::WebInputEvent::GestureScrollBegin:
|
case blink::WebInputEvent::GestureScrollBegin:
|
||||||
|
|
|
@ -31,6 +31,8 @@
|
||||||
- (NSTouchBar*)touchBarFromItemIdentifiers:(NSMutableArray*)items;
|
- (NSTouchBar*)touchBarFromItemIdentifiers:(NSMutableArray*)items;
|
||||||
- (NSMutableArray*)identifiersFromSettings:(const std::vector<mate::PersistentDictionary>&)settings;
|
- (NSMutableArray*)identifiersFromSettings:(const std::vector<mate::PersistentDictionary>&)settings;
|
||||||
- (void)refreshTouchBarItem:(NSTouchBar*)touchBar id:(const std::string&)item_id;
|
- (void)refreshTouchBarItem:(NSTouchBar*)touchBar id:(const std::string&)item_id;
|
||||||
|
- (void)addNonDefaultTouchBarItems:(std::vector<mate::PersistentDictionary>)items;
|
||||||
|
- (void)setEscapeTouchBarItem:(mate::PersistentDictionary)item forTouchBar:(NSTouchBar*)touchBar;
|
||||||
|
|
||||||
|
|
||||||
- (NSString*)idFromIdentifier:(NSString*)identifier withPrefix:(NSString*)prefix;
|
- (NSString*)idFromIdentifier:(NSString*)identifier withPrefix:(NSString*)prefix;
|
||||||
|
|
|
@ -145,7 +145,25 @@ static NSString* const ImageScrubberItemIdentifier = @"scrubber.image.item";
|
||||||
} else if (item_type == "scrubber") {
|
} else if (item_type == "scrubber") {
|
||||||
[self updateScrubber:(NSCustomTouchBarItem*)item withSettings:settings];
|
[self updateScrubber:(NSCustomTouchBarItem*)item withSettings:settings];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)addNonDefaultTouchBarItems:(std::vector<mate::PersistentDictionary>)items {
|
||||||
|
[self identifiersFromSettings:items];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)setEscapeTouchBarItem:(mate::PersistentDictionary)item forTouchBar:(NSTouchBar*)touchBar {
|
||||||
|
std::string type;
|
||||||
|
std::string item_id;
|
||||||
|
NSTouchBarItemIdentifier identifier = nil;
|
||||||
|
if (item.Get("type", &type) && item.Get("id", &item_id)) {
|
||||||
|
identifier = [self identifierFromID:item_id type:type];
|
||||||
|
}
|
||||||
|
if (identifier) {
|
||||||
|
[self addNonDefaultTouchBarItems:{ item }];
|
||||||
|
touchBar.escapeKeyReplacementItemIdentifier = identifier;
|
||||||
|
} else {
|
||||||
|
touchBar.escapeKeyReplacementItemIdentifier = nil;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)buttonAction:(id)sender {
|
- (void)buttonAction:(id)sender {
|
||||||
|
|
|
@ -6,7 +6,7 @@ Process: [Main](../tutorial/quick-start.md#main-process)
|
||||||
|
|
||||||
### `new TouchBar(items)` _Experimental_
|
### `new TouchBar(items)` _Experimental_
|
||||||
|
|
||||||
* `items` ([TouchBarButton](touch-bar-button.md) | [TouchBarColorPicker](touch-bar-color-picker.md) | [TouchBarGroup](touch-bar-group.md) | [TouchBarLabel](touch-bar-label.md) | [TouchBarPopover](touch-bar-popover.md) | [TouchBarSlider](touch-bar-slider.md) | [TouchBarSpacer](touch-bar-spacer.md))[]
|
* `items` ([TouchBarButton](touch-bar-button.md) | [TouchBarColorPicker](touch-bar-color-picker.md) | [TouchBarGroup](touch-bar-group.md) | [TouchBarLabel](touch-bar-label.md) | [TouchBarPopover](touch-bar-popover.md) | [TouchBarScrubber](touch-bar-scrubber.md) | [TouchBarSegmentedControl](touch-bar-segmented-control.md) | [TouchBarSlider](touch-bar-slider.md) | [TouchBarSpacer](touch-bar-spacer.md))[]
|
||||||
|
|
||||||
Creates a new touch bar with the specified items. Use
|
Creates a new touch bar with the specified items. Use
|
||||||
`BrowserWindow.setTouchBar` to add the `TouchBar` to a window.
|
`BrowserWindow.setTouchBar` to add the `TouchBar` to a window.
|
||||||
|
@ -14,6 +14,17 @@ Creates a new touch bar with the specified items. Use
|
||||||
**Note:** The TouchBar API is currently experimental and may change or be
|
**Note:** The TouchBar API is currently experimental and may change or be
|
||||||
removed in future Electron releases.
|
removed in future Electron releases.
|
||||||
|
|
||||||
|
### Instance Methods
|
||||||
|
|
||||||
|
The following methods are available on instances of `TouchBar`:
|
||||||
|
|
||||||
|
#### `touchBar.replaceEscapeItem([touchBarItem])`
|
||||||
|
|
||||||
|
* `touchBarItem` (TouchBarButton | TouchBarColorPicker | TouchBarGroup | TouchBarLabel | TouchBarPopover | TouchBarScrubber | TouchBarSegmentedControl | TouchBarSlider | TouchBarSpacer) - (Optional) The touch bar item to replace the escape button with
|
||||||
|
|
||||||
|
Replaces the "esc" button on the touchbar with the given TouchBarItem, if `touchBarItem` is not provided or is falsey the button is reset
|
||||||
|
to the "esc" button automatically.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
Below is an example of a simple slot machine touch bar game with a button
|
Below is an example of a simple slot machine touch bar game with a button
|
||||||
|
|
|
@ -28,6 +28,7 @@ class TouchBar extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.windowListeners = {}
|
this.windowListeners = {}
|
||||||
|
this.windows = {}
|
||||||
this.items = {}
|
this.items = {}
|
||||||
this.ordereredItems = []
|
this.ordereredItems = []
|
||||||
|
|
||||||
|
@ -49,6 +50,15 @@ class TouchBar extends EventEmitter {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
replaceEscapeItem(item) {
|
||||||
|
if (!item) item = {}
|
||||||
|
Object.keys(this.windows).forEach((windowID) => {
|
||||||
|
const window = this.windows[windowID]
|
||||||
|
window._setEscapeTouchBarItem(item)
|
||||||
|
})
|
||||||
|
this._escape = item;
|
||||||
|
}
|
||||||
|
|
||||||
_addToWindow (window) {
|
_addToWindow (window) {
|
||||||
const {id} = window
|
const {id} = window
|
||||||
|
|
||||||
|
@ -76,11 +86,15 @@ class TouchBar extends EventEmitter {
|
||||||
window.removeListener('closed', removeListeners)
|
window.removeListener('closed', removeListeners)
|
||||||
window._touchBar = null
|
window._touchBar = null
|
||||||
delete this.windowListeners[id]
|
delete this.windowListeners[id]
|
||||||
|
delete this.windows[id]
|
||||||
}
|
}
|
||||||
window.once('closed', removeListeners)
|
window.once('closed', removeListeners)
|
||||||
this.windowListeners[id] = removeListeners
|
this.windowListeners[id] = removeListeners
|
||||||
|
this.windows[id] = window;
|
||||||
|
|
||||||
window._setTouchBarItems(this.ordereredItems)
|
window._setTouchBarItems(this.ordereredItems)
|
||||||
|
|
||||||
|
if (this._escape) window._setEscapeTouchBarItem(this._escape)
|
||||||
}
|
}
|
||||||
|
|
||||||
_removeFromWindow (window) {
|
_removeFromWindow (window) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue