Skip to content

Commit

Permalink
Add Touch Bar controls
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbonner committed Oct 6, 2023
1 parent 54a011e commit 749b0fe
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
Binary file added icons/touch-bar-plus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 28 additions & 2 deletions src/hub/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { AdvantageScopeAssets } from "../shared/AdvantageScopeAssets";
import { HubState } from "../shared/HubState";
import { SIM_ADDRESS, USB_ADDRESS } from "../shared/IPAddresses";
import Log from "../shared/log/Log";
import { getEnabledData, searchFields } from "../shared/log/LogUtil";
import { getEnabledData } from "../shared/log/LogUtil";
import NamedMessage from "../shared/NamedMessage";
import Preferences from "../shared/Preferences";
import { htmlEncode } from "../shared/util";
import { clampValue, htmlEncode, scaleValue } from "../shared/util";
import { HistoricalDataSource, HistoricalDataSourceStatus } from "./dataSources/HistoricalDataSource";
import { LiveDataSource, LiveDataSourceStatus } from "./dataSources/LiveDataSource";
import loadZebra from "./dataSources/LoadZebra";
Expand Down Expand Up @@ -368,6 +368,23 @@ UPDATE_BUTTON.addEventListener("click", () => {
window.sendMainMessage("prompt-update");
});

// Update touch bar slider position
setInterval(() => {
if (window.platform === "darwin") {
let range = window.log.getTimestampRange();
let liveTime = window.selection.getCurrentLiveTime();
if (liveTime !== null) {
range[1] = liveTime;
}
let selectedTime = window.selection.getSelectedTime();
if (selectedTime === null) {
selectedTime = range[0];
}
let timePercent = clampValue(scaleValue(selectedTime, range, [0, 1]), 0, 1);
window.sendMainMessage("update-touch-bar-slider", timePercent);
}
}, 1000 / 60);

function handleMainMessage(message: NamedMessage) {
switch (message.name) {
case "restore-state":
Expand Down Expand Up @@ -603,6 +620,15 @@ function handleMainMessage(message: NamedMessage) {
setLoading(null);
break;

case "update-touch-bar-slider":
let range = window.log.getTimestampRange();
let liveTime = window.selection.getCurrentLiveTime();
if (liveTime !== null) {
range[1] = liveTime;
}
window.selection.setSelectedTime(scaleValue(message.data, [0, 1], range));
break;

default:
console.warn("Unknown message from main process", message);
break;
Expand Down
59 changes: 59 additions & 0 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import {
MenuItem,
MessageChannelMain,
MessagePortMain,
TouchBar,
TouchBarSlider,
app,
dialog,
nativeImage,
nativeTheme,
powerMonitor,
shell
Expand Down Expand Up @@ -56,6 +59,7 @@ let prefsWindow: BrowserWindow | null = null;
let licensesWindow: BrowserWindow | null = null;
let satelliteWindows: { [id: string]: BrowserWindow[] } = {};
let windowPorts: { [id: number]: MessagePortMain } = {};
let hubTouchBarSliders: { [id: number]: TouchBarSlider } = {};

let hubStateTracker = new StateTracker();
let updateChecker = new UpdateChecker();
Expand Down Expand Up @@ -457,6 +461,13 @@ function handleHubMessage(window: BrowserWindow, message: NamedMessage) {
);
break;

case "update-touch-bar-slider":
if (window.id in hubTouchBarSliders) {
let slider = hubTouchBarSliders[window.id];
slider.value = Math.round(message.data * slider.maxValue);
}
break;

default:
console.warn("Unknown message from hub renderer process", message);
break;
Expand Down Expand Up @@ -1328,6 +1339,54 @@ function createHubWindow() {
let window = new BrowserWindow(prefs);
hubWindows.push(window);

// Add touch bar menu
let resetTouchBar = () => {
let newCreated = false;
let slider = new TouchBar.TouchBarSlider({
value: window.id in hubTouchBarSliders ? hubTouchBarSliders[window.id].value : 0,
minValue: 0,
maxValue: 10000,
change(newValue) {
sendMessage(window, "update-touch-bar-slider", newValue / slider.maxValue);
}
});
hubTouchBarSliders[window.id] = slider;
window.setTouchBar(
new TouchBar({
items: [
new TouchBar.TouchBarOtherItemsProxy(),
new TouchBar.TouchBarPopover({
icon: nativeImage.createFromPath(path.join(__dirname, "../icons/touch-bar-plus.png")),
showCloseButton: true,
items: new TouchBar({
items: [
new TouchBar.TouchBarScrubber({
selectedStyle: "background",
continuous: false,
items: getAllTabTypes()
.slice(1)
.map((type) => {
return {
label: getTabIcon(type) + " " + getDefaultTabTitle(type)
};
}),
select(index) {
if (newCreated) return;
newCreated = true;
sendMessage(window, "new-tab", index + 1);
setTimeout(resetTouchBar, 350);
}
})
]
})
}),
slider
]
})
);
};
resetTouchBar();

// Show window when loaded
window.once("ready-to-show", window.show);

Expand Down

0 comments on commit 749b0fe

Please sign in to comment.