Skip to content

Commit

Permalink
add a way to easly add a touchbar widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Wojciech Połowniak committed Jul 29, 2018
1 parent d33becc commit 51e1161
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import * as uuidv5 from 'uuid/v5';

let fetch: any;
let deleteTriggerFn: any; // could be "method" = 'webserver' | 'url scheme'
let getBinaryPath: any;

const NAMESPACE = '87a84aef-11fe-4dce-8d00-429cea46f345';

if (DetectNode) {
fetch = require('node-fetch-polyfill');
deleteTriggerFn = require('../backend/util').deleteTrigger;
getBinaryPath = require('../backend/util').nodeBinaryPath;
} else {
fetch = window.fetch;
deleteTriggerFn = require('../frontend/util').deleteTrigger;
getBinaryPath = (): any => undefined;
}

export function deleteTrigger(uuid: string): void {
Expand Down Expand Up @@ -180,6 +185,20 @@ function getTriggerClassProperty(value: number): string {
* @param text
* @param namespace
*/
export function generateUuidForString(text: string, namespace: string): string {
export function generateUuidForString(text: string, namespace: string = NAMESPACE): string {
return uuidv5(String(text), namespace);
}

/**
* Returns the current UUID representing the namespace of the package
*/
export function getNamespace(): string {
return NAMESPACE;
}

/**
* Returns the path for the current node binary (or undefined on frontend)
*/
export function getNodeBinaryPath(): string {
return getBinaryPath();
}
55 changes: 54 additions & 1 deletion src/common/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,58 @@ export class FWidget {
*/
public get(config: Types.IWidgetConfig): Widget {
return new Widget(this.config, config);
}
}

/**
* Creates a new BetterTouchTool touchbar widget and returns its instance
* @param options: Types.ITouchbarWidgetCreateConfig
*/
public async create(options: Types.ITouchbarWidgetCreateConfig): Promise<any> {
const uuid = CommonUtils.generateUuidForString(JSON.stringify(options));

const binaryPath = CommonUtils.getNodeBinaryPath();

if (!binaryPath && !options.path) {
console.error('Sorry, you have to provide the node/bash binary path manually in the params');
return;
}

// path to the executable, with slashes escaped
const escapedPath = (options.path ? options.path : binaryPath).replace(/\//g, '\/');

// btt format for executable path
const shellScriptWidgetGestureConfig = `${escapedPath}:::${(options.mode === 'node' ? '-e' : '-c')}`;

// real payload that'll create a widget
const BTTPayload: any = {
"BTTWidgetName" : options.name,
"BTTTriggerType" : Types.TOUCHBAR_WIDGETS.CREATE,
"BTTTriggerClass" : "BTTTriggerTypeTouchBar",
"BTTPredefinedActionType" : -1,
"BTTPredefinedActionName" : "No Action",
"BTTShellScriptWidgetGestureConfig" : shellScriptWidgetGestureConfig,
"BTTEnabled2" : 1,
"BTTUUID" : uuid,
"BTTEnabled" : 1,
"BTTOrder" : 9999,
"BTTTriggerConfig" : {
"BTTTouchBarItemIconHeight" : options.appearance.iconHeight,
"BTTTouchBarItemIconWidth" : options.appearance.iconWidth,
"BTTTouchBarItemPadding" : options.appearance.padding,
"BTTTouchBarFreeSpaceAfterButton" : String(options.appearance.freeSpaceAfterButton),
"BTTTouchBarButtonColor" : options.appearance.buttonColor,
"BTTTouchBarAlwaysShowButton" : String(Number(options.alwaysShow)),
"BTTTouchBarShellScriptString" : options.script,
"BTTTouchBarAlternateBackgroundColor" : options.appearance.alternateBackgroundColor
},
};

// make the request to the BTT API to create new widget
await CommonUtils.makeAction('add_new_trigger', {
json: JSON.stringify(BTTPayload),
}, this.config);

// get the instance representing the newly created widget
return new Widget(this.config, { uuid, default: undefined });
}
}
20 changes: 20 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ export enum TRACKPAD_TRIGGERS {
FIVE_FINGER_FORCE_CLICK = 172,
}

export enum TOUCHBAR_WIDGETS {
CREATE = 642,
}

export enum KEYBOARD_MODIFIERS {

}
Expand Down Expand Up @@ -243,4 +247,20 @@ export interface IEventCallback {
comment: string;
}

export interface ITouchbarWidgetCreateConfig {
name: string; // 'toucbar widget name',
mode: 'node' | 'bash';
path: string;
alwaysShow: boolean;
script: string; // "console.log('foo');",
appearance: {
iconHeight: number; // 22,
iconWidth: number; // 22,
padding: number; // -5,
freeSpaceAfterButton: number; // "5.000000",
buttonColor: string; // "0.000000, 0.000000, 0.000000, 255.000000",
alternateBackgroundColor: string; // "128.829533, 128.829533, 128.829533, 255.000000"
},
}

export type ActionJSON = Record<string, any>;

0 comments on commit 51e1161

Please sign in to comment.