Skip to content

Commit

Permalink
move addTooltip to ToolBarButton
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Apr 17, 2020
1 parent a0cc485 commit 3ed376b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 44 deletions.
44 changes: 42 additions & 2 deletions lib/items/tool-bar-button-view.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {CompositeDisposable} = require('atom');
const { ToolBarItem, addTooltip } = require('./tool-bar-item');
const { ToolBarItem } = require('./tool-bar-item');

/**
* A button class with many options
Expand Down Expand Up @@ -46,7 +46,7 @@ class ToolBarButtonView extends ToolBarItem {
if (options.tooltip) {
this.subscriptions = new CompositeDisposable();
this.subscriptions.add(
addTooltip(this.options.tooltip, this.options.callback)
this.addTooltip(this.options.tooltip, this.options.callback)
);
}

Expand Down Expand Up @@ -99,6 +99,35 @@ class ToolBarButtonView extends ToolBarItem {
}
}

/**
* adds a Tooltip for your item.
* @param {ButtonOptions.tooltip} tooltipOptions
* @param {ButtonOptions.callback | null} callback
* @returns {Disposable} a disposable tooltip
*/
addTooltip (tooltipOptions, callback = null) {
let tooltip;
if (typeof tooltipOptions === 'string') {
tooltip = {
title: tooltipOptions
};
} else {
tooltip = tooltipOptions;
}

if (!tooltip.hasOwnProperty('placement')) {
tooltip.placement = getTooltipPlacement();
}

if (!tooltip.hasOwnProperty('keyBindingCommand') &&
typeof callback === 'string'
) {
tooltip.keyBindingCommand = callback;
}

return atom.tooltips.add(this.element, tooltip);
}

/** Add color to the button */
addColor () {
this.element.style.color = this.options.color;
Expand Down Expand Up @@ -218,4 +247,15 @@ function getCallbackModifier (callback, {altKey, ctrlKey, shiftKey}) {
return callback[modifier] || callback[''];
}

/** get the tooltip placement based on the toolbar position */
function getTooltipPlacement () {
const toolbarPosition = atom.config.get('tool-bar.position');

return toolbarPosition === 'Top' ? 'bottom'
: toolbarPosition === 'Right' ? 'left'
: toolbarPosition === 'Bottom' ? 'top'
: toolbarPosition === 'Left' ? 'right'
: null;
}

module.exports.ToolBarButtonView = ToolBarButtonView;
42 changes: 0 additions & 42 deletions lib/items/tool-bar-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,45 +26,3 @@ class ToolBarItem {
}

module.exports.ToolBarItem = ToolBarItem;

/**
* adds a Tooltip for your item.
* @param {ButtonOptions.tooltip} tooltipOptions
* @param {ButtonOptions.callback | null} callback
* @returns {Disposable} a disposable tooltip
*/
function addTooltip (tooltipOptions, callback = null) {
let tooltip;
if (typeof tooltipOptions === 'string') {
tooltip = {
title: tooltipOptions
};
} else {
tooltip = tooltipOptions;
}

if (!tooltip.hasOwnProperty('placement')) {
tooltip.placement = getTooltipPlacement();
}

if (!tooltip.hasOwnProperty('keyBindingCommand') &&
typeof callback === 'string'
) {
tooltip.keyBindingCommand = callback;
}

return atom.tooltips.add(this.element, tooltip);
}

/** get the tooltip placement based on the toolbar position */
function getTooltipPlacement () {
const toolbarPosition = atom.config.get('tool-bar.position');

return toolbarPosition === 'Top' ? 'bottom'
: toolbarPosition === 'Right' ? 'left'
: toolbarPosition === 'Bottom' ? 'top'
: toolbarPosition === 'Left' ? 'right'
: null;
}

module.exports.addTooltip = addTooltip;

0 comments on commit 3ed376b

Please sign in to comment.