mirror of https://github.com/buggins/dlangui.git
tooltips support
This commit is contained in:
parent
1b338828af
commit
08e9cd8d06
|
@ -215,6 +215,22 @@ class Action {
|
|||
return null;
|
||||
return _accelerators[0].label;
|
||||
}
|
||||
/// returns tooltip text for action
|
||||
@property dstring tooltipText() {
|
||||
dchar[] buf;
|
||||
// strip out & characters
|
||||
foreach(ch; label) {
|
||||
if (ch != '&')
|
||||
buf ~= ch;
|
||||
}
|
||||
dstring accel = acceleratorText;
|
||||
if (accel.length > 0) {
|
||||
buf ~= " (";
|
||||
buf ~= accel;
|
||||
buf ~= ")";
|
||||
}
|
||||
return cast(dstring)buf;
|
||||
}
|
||||
/// adds one more accelerator
|
||||
Action addAccelerator(uint keyCode, uint keyFlags = 0) {
|
||||
_accelerators ~= Accelerator(keyCode, keyFlags);
|
||||
|
|
|
@ -126,6 +126,12 @@ struct UIString {
|
|||
_value = null;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// returns true if string is empty: neither resource nor string is assigned
|
||||
bool empty() const {
|
||||
return _value.length == 0 && _id.length == 0;
|
||||
}
|
||||
|
||||
/** Default conversion to dstring */
|
||||
alias value this;
|
||||
}
|
||||
|
|
|
@ -252,17 +252,18 @@ class Window {
|
|||
_tooltip.timerId = setTimer(ownerWidget, delay);
|
||||
}
|
||||
|
||||
/// call when tooltip timer is expired
|
||||
private bool onTooltipTimer() {
|
||||
_tooltip.timerId = 0;
|
||||
if (isChild(_tooltip.ownerWidget)) {
|
||||
Widget w = _tooltip.ownerWidget.createTooltip();
|
||||
Widget w = _tooltip.ownerWidget.createTooltip(_lastMouseX, _lastMouseY, _tooltip.alignment, _tooltip.x, _tooltip.y);
|
||||
if (w)
|
||||
showTooltip(w, _tooltip.ownerWidget, _tooltip.alignment, _tooltip.x, _tooltip.y);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// hide tooltip if shown
|
||||
/// hide tooltip if shown and cancel tooltip timer if set
|
||||
void hideTooltip() {
|
||||
if (_tooltip.popup) {
|
||||
destroy(_tooltip.popup);
|
||||
|
@ -270,6 +271,8 @@ class Window {
|
|||
if (_mainWidget)
|
||||
_mainWidget.invalidate();
|
||||
}
|
||||
if (_tooltip.timerId)
|
||||
cancelTimer(_tooltip.timerId);
|
||||
}
|
||||
|
||||
/// show tooltip immediately
|
||||
|
@ -506,6 +509,7 @@ class Window {
|
|||
/// dispatch keyboard event
|
||||
bool dispatchKeyEvent(KeyEvent event) {
|
||||
bool res = false;
|
||||
hideTooltip();
|
||||
PopupWidget modal = modalPopup();
|
||||
if (event.action == KeyAction.KeyDown || event.action == KeyAction.KeyUp) {
|
||||
_keyboardModifiers = event.flags;
|
||||
|
@ -783,12 +787,18 @@ class Window {
|
|||
return false;
|
||||
}
|
||||
|
||||
private int _lastMouseX;
|
||||
private int _lastMouseY;
|
||||
/// dispatch mouse event to window content widgets
|
||||
bool dispatchMouseEvent(MouseEvent event) {
|
||||
// ignore events if there is no root
|
||||
if (_mainWidget is null)
|
||||
return false;
|
||||
|
||||
if (event.action == MouseAction.Move) {
|
||||
_lastMouseX = event.x;
|
||||
_lastMouseY = event.y;
|
||||
}
|
||||
hideTooltip();
|
||||
|
||||
PopupWidget modal = modalPopup();
|
||||
|
|
|
@ -144,6 +144,10 @@ immutable string STYLE_TAB_UP_BUTTON_DARK = "TAB_UP_BUTTON_DARK";
|
|||
/// standard style id for tab control tab button text in dock frame
|
||||
immutable string STYLE_TAB_UP_BUTTON_DARK_TEXT = "TAB_UP_BUTTON_DARK_TEXT";
|
||||
|
||||
/// standard style id for tooltip popup
|
||||
immutable string STYLE_TOOLTIP = "TOOLTIP";
|
||||
|
||||
|
||||
/// standard style id for toolbars layout
|
||||
immutable string STYLE_TOOLBAR_HOST = "TOOLBAR_HOST";
|
||||
/// standard style id for toolbars
|
||||
|
|
|
@ -67,6 +67,7 @@ class ToolBarImageButton : ImageButton {
|
|||
styleId = STYLE_TOOLBAR_BUTTON;
|
||||
focusable = false;
|
||||
}
|
||||
mixin ActionTooltipSupport;
|
||||
}
|
||||
|
||||
/// separator for toolbars
|
||||
|
@ -85,6 +86,7 @@ class ToolBarComboBox : ComboBox {
|
|||
if (items.length > 0)
|
||||
selectedItemIndex = 0;
|
||||
}
|
||||
mixin ActionTooltipSupport;
|
||||
}
|
||||
|
||||
/// Layout with buttons
|
||||
|
|
|
@ -683,15 +683,16 @@ class Widget {
|
|||
}
|
||||
|
||||
/// returns true if widget has tooltip to show
|
||||
bool hasTooltip() {
|
||||
@property bool hasTooltip() {
|
||||
return false;
|
||||
}
|
||||
/// will be called from window once tooltip request timer expired
|
||||
Widget createTooltip() {
|
||||
/// will be called from window once tooltip request timer expired; if null is returned, popup will not be shown; you can change alignment and position of popup here
|
||||
Widget createTooltip(int mouseX, int mouseY, ref uint alignment, ref int x, ref int y) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/// schedule tooltip
|
||||
void scheduleTooltip(long delay = 300, uint alignment = 2 /*PopupAlign.Below*/, int x = 0, int y = 0) {
|
||||
protected void scheduleTooltip(long delay = 300, uint alignment = 2 /*PopupAlign.Below*/, int x = 0, int y = 0) {
|
||||
if (auto w = window)
|
||||
w.scheduleTooltip(this, delay, alignment, x, y);
|
||||
}
|
||||
|
@ -1106,6 +1107,9 @@ class Widget {
|
|||
return true;
|
||||
}
|
||||
}
|
||||
if (event.action == MouseAction.Move && event.flags == 0 && hasTooltip) {
|
||||
scheduleTooltip(200);
|
||||
}
|
||||
if (event.action == MouseAction.ButtonDown && event.button == MouseButton.Right) {
|
||||
if (canShowPopupMenu(event.x, event.y)) {
|
||||
showPopupMenu(event.x, event.y);
|
||||
|
@ -1553,3 +1557,21 @@ struct AnimationHelper {
|
|||
return _timeElapsed >= _maxInterval;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// mixin this to widget class to support tooltips based on widget's action label
|
||||
mixin template ActionTooltipSupport() {
|
||||
/// returns true if widget has tooltip to show
|
||||
override @property bool hasTooltip() {
|
||||
if (!_action || _action.labelValue.empty)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
/// will be called from window once tooltip request timer expired; if null is returned, popup will not be shown; you can change alignment and position of popup here
|
||||
override Widget createTooltip(int mouseX, int mouseY, ref uint alignment, ref int x, ref int y) {
|
||||
Widget res = new TextWidget("tooltip", _action.tooltipText);
|
||||
res.styleId = STYLE_TOOLTIP;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 562 B |
|
@ -300,6 +300,14 @@
|
|||
layoutHeight="WRAP_CONTENT"
|
||||
padding="1,1,1,1"
|
||||
/>
|
||||
<style id="TOOLTIP"
|
||||
backgroundImageId="tooltip_background"
|
||||
layoutWidth="WRAP_CONTENT"
|
||||
layoutHeight="WRAP_CONTENT"
|
||||
margins="2,7,2,1"
|
||||
padding="3,3,3,3"
|
||||
textColor="#404040"
|
||||
/>
|
||||
<style id="TOOLBAR"
|
||||
backgroundImageId="toolbar_background"
|
||||
layoutWidth="WRAP_CONTENT"
|
||||
|
|
|
@ -85,6 +85,7 @@ res/mdpi/toolbar_button_pressed.9.png
|
|||
res/mdpi/toolbar_control_disabled.9.png
|
||||
res/mdpi/toolbar_control_normal.9.png
|
||||
res/mdpi/toolbar_separator.png
|
||||
res/mdpi/tooltip_background.9.png
|
||||
res/mdpi/user-home.png
|
||||
res/menu_item_background.xml
|
||||
res/popup_menu_background_normal.9.png
|
||||
|
|
Loading…
Reference in New Issue