Skip to content

Commit

Permalink
Merge pull request #576 from laeubi/slider_support_default_seleect
Browse files Browse the repository at this point in the history
Add support for the default selection to the slider
  • Loading branch information
lcaron authored Mar 18, 2024
2 parents ee2548d + ab331a6 commit 476fe0f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class NebulaSlider extends Canvas {

private IntFunction<String> format;

private int movingValue;

/**
* Constructs a new instance of this class given its parent and a style value
* describing its behavior and appearance.
Expand Down Expand Up @@ -229,12 +231,16 @@ private void addMouseListeners() {
return;
}
moving = true;
movingValue = value;
mouseDeltaX = xPosition - e.x;
});

addListener(SWT.MouseUp, e -> {
moving = false;
mouseDeltaX = 0;
if(movingValue != value) {
SelectionListenerUtil.fireDefaultSelectionListeners(this, e);
}
redraw();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,23 @@ public static void removeSelectionListener(final Control control, final Selectio
* @return true if the selection could be changed, false otherwise
*/
public static boolean fireSelectionListeners(final Control control, final Event sourceEvent) {
for (final Listener listener : control.getListeners(SWT.Selection)) {
return fireSelectionListenersEvent(control, sourceEvent, SWT.Selection);
}

/**
* Fire the default selection listeners of a given control
*
* @param control the control that fires the event
* @param sourceEvent mouse event
* @return true if the selection could be changed, false otherwise
*/
public static boolean fireDefaultSelectionListeners(final Control control, final Event sourceEvent) {
return fireSelectionListenersEvent(control, sourceEvent, SWT.DefaultSelection);
}

private static boolean fireSelectionListenersEvent(final Control control, final Event sourceEvent, int type) {
Listener[] listeners = control.getListeners(SWT.Selection);
for(final Listener listener : listeners) {
final Event event = new Event();

event.button = sourceEvent==null?1:sourceEvent.button;
Expand All @@ -76,7 +92,7 @@ public static boolean fireSelectionListeners(final Control control, final Event
event.time = sourceEvent == null ? 0 : sourceEvent.time;
event.x = sourceEvent == null ? 0 : sourceEvent.x;
event.y = sourceEvent == null ? 0 : sourceEvent.y;
event.type = SWT.Selection;
event.type = type;

listener.handleEvent(event);
if (!event.doit) {
Expand Down

0 comments on commit 476fe0f

Please sign in to comment.