use of com.github.weisj.darklaf.components.tooltip.ToolTipContext in project darklaf by weisJ.
the class ThemeSettingsUI method createFontSlider.
private static JSlider createFontSlider() {
JSlider fontSlider = new JSlider() {
@Override
public String getToolTipText(final MouseEvent event) {
return getValue() + "%";
}
};
ToolTipContext context = new ToolTipContext().setAlignment(Alignment.CENTER).setCenterAlignment(Alignment.NORTH).setUseBestFit(true).setToolTipRectSupplier(e -> {
SliderUI ui = fontSlider.getUI();
if (ui instanceof DarkSliderUI) {
Rectangle r = ((DarkSliderUI) ui).getThumbRect();
r.x -= 1;
return r;
}
return new Rectangle(0, 0, fontSlider.getWidth(), fontSlider.getHeight());
});
fontSlider.putClientProperty(DarkSliderUI.KEY_INSTANT_SCROLL, true);
fontSlider.putClientProperty(DarkSliderUI.KEY_USE_TRACK_AS_BASELINE, true);
fontSlider.putClientProperty(ToolTipConstants.KEY_CONTEXT, context);
fontSlider.putClientProperty(ToolTipConstants.KEY_STYLE, ToolTipConstants.VARIANT_BALLOON);
fontSlider.setToolTipText(String.valueOf(fontSlider.getValue()));
fontSlider.setOrientation(JSlider.HORIZONTAL);
fontSlider.setPaintTicks(true);
fontSlider.setMinimum(FontSizePreset.TINY.getPercentage());
fontSlider.setMaximum(FontSizePreset.HUGE.getPercentage());
int tickSpacing = 25;
Dictionary<Integer, JComponent> dict = fontSlider.createStandardLabels(tickSpacing);
JLabel min = (JLabel) dict.get(fontSlider.getMinimum());
UIUpdater.registerComponent(min);
min.setText("");
min.setIcon(AllIcons.Action.DecreaseFontSize.get());
min.setAlignmentX(JComponent.CENTER_ALIGNMENT);
min.putClientProperty(DarkSliderUI.KEY_MANUAL_LABEL_ALIGN, true);
JLabel mid = (JLabel) dict.get(fontSlider.getMinimum() + tickSpacing);
UIUpdater.registerComponent(mid);
dict.remove(fontSlider.getMinimum() + tickSpacing);
dict.put(FontSizePreset.NORMAL.getPercentage(), mid);
DynamicUI.withDynamic(mid, c -> c.setText(UIManager.getString("settings.label_font_default", fontSlider.getLocale())));
mid.setAlignmentX(JComponent.CENTER_ALIGNMENT);
mid.setHorizontalTextPosition(JLabel.RIGHT);
JLabel max = (JLabel) dict.get(fontSlider.getMaximum());
max.putClientProperty(DarkSliderUI.KEY_MANUAL_LABEL_ALIGN, true);
max.setText("");
max.setIcon(AllIcons.Action.IncreaseFontSize.get());
max.setAlignmentX(JComponent.CENTER_ALIGNMENT);
max.putClientProperty(DarkSliderUI.KEY_MANUAL_LABEL_ALIGN, true);
UIUpdater.registerComponent(max);
dict.remove(FontSizePreset.Small.getPercentage());
dict.remove(FontSizePreset.SMALL.getPercentage());
dict.remove(FontSizePreset.Large.getPercentage());
dict.remove(FontSizePreset.LARGE.getPercentage());
dict.remove(FontSizePreset.Huge.getPercentage());
fontSlider.setLabelTable(dict);
fontSlider.setMajorTickSpacing(tickSpacing);
fontSlider.setMinorTickSpacing(tickSpacing);
fontSlider.setPaintLabels(true);
fontSlider.setSnapToTicks(true);
return fontSlider;
}
use of com.github.weisj.darklaf.components.tooltip.ToolTipContext in project darklaf by weisJ.
the class ToolTipDemo method createComponent.
@Override
public JComponent createComponent() {
JButton button = new JButton("Demo Button", DemoResources.FOLDER_ICON);
DemoPanel panel = new DemoPanel(button);
ToolTipContext context = new ToolTipContext(button).setAlignment(Alignment.CENTER).setCenterAlignment(Alignment.SOUTH);
ToolTipContext.setDefaultContext(context);
button.setToolTipText(StringUtil.toHtml("<p style=\"color:red;\">This is the ToolTip demo text!\n" + "<p style=\"color:blue;\">This is the ToolTip demo text!\n" + "<p style=\"color:green;\">This is the ToolTip demo text!\n"));
button.putClientProperty(DarkToolTipUI.KEY_STYLE, DarkToolTipUI.VARIANT_BALLOON);
button.putClientProperty(DarkToolTipUI.KEY_CONTEXT, context);
JPanel controlPanel = panel.addControls(3);
controlPanel.add(new JCheckBox("Align inside") {
{
setSelected(context.isAlignInside());
addActionListener(e -> context.setAlignInside(isSelected()));
}
});
controlPanel.add(new JCheckBox("Ignore Border") {
{
setSelected(context.isIgnoreBorder());
addActionListener(e -> context.setIgnoreBorder(isSelected()));
}
});
controlPanel.add(new JCheckBox("Use best fit") {
{
setSelected(context.isBestFit());
addActionListener(e -> context.setUseBestFit(isSelected()));
}
});
controlPanel = panel.addControls();
controlPanel.add(new JLabel("Tooltip Style:", JLabel.RIGHT));
controlPanel.add(new JComboBox<ToolTipStyle>(ToolTipStyle.values()) {
{
setSelectedItem(ToolTipStyle.BALLOON);
addItemListener(e -> button.putClientProperty(DarkToolTipUI.KEY_STYLE, e.getItem()));
}
}, "sgx");
controlPanel.add(new JLabel("Alignment:", JLabel.RIGHT));
controlPanel.add(new JComboBox<Alignment>(Alignment.values()) {
{
setSelectedItem(context.getAlignment());
addItemListener(e -> context.setAlignment((Alignment) e.getItem()));
}
}, "sgx");
controlPanel.add(new JLabel("Center Alignment:", JLabel.RIGHT));
controlPanel.add(new JComboBox<Alignment>(Alignment.values()) {
{
setSelectedItem(context.getCenterAlignment());
addItemListener(e -> context.setCenterAlignment((Alignment) e.getItem()));
}
}, "sgx");
controlPanel.add(new JLabel("Alignment Strategy:", JLabel.RIGHT));
controlPanel.add(new JComboBox<AlignmentStrategy>(AlignmentStrategy.values()) {
{
setSelectedItem(context.getAlignmentStrategy());
addItemListener(e -> context.setAlignmentStrategy((AlignmentStrategy) e.getItem()));
}
}, "sgx");
return panel;
}
use of com.github.weisj.darklaf.components.tooltip.ToolTipContext in project darklaf by weisJ.
the class AttachedPopupComponent method showComponentImpl.
public static void showComponentImpl(final JComponent parent, final JComponent content, final Runnable onClose, final Runnable onAbort, final Runnable afterClose) {
AttachedPopupComponent attachedComp = new AttachedPopupComponent(parent, content);
/*
* Position is (0,0) as the ToolTipContext figures out the correct location.
*/
final Popup popup = PopupFactory.getSharedInstance().getPopup(parent, attachedComp, 0, 0);
popup.show();
Window window = DarkUIUtil.getWindow(parent);
AtomicReference<BiConsumer<AWTEvent, Boolean>> close = new AtomicReference<>();
AtomicReference<Runnable> listenerRemover = new AtomicReference<>();
AtomicBoolean open = new AtomicBoolean(true);
ComponentListener windowListener = new ComponentAdapter() {
@Override
public void componentMoved(final ComponentEvent e) {
close.get().accept(e, true);
listenerRemover.get().run();
}
@Override
public void componentResized(final ComponentEvent e) {
close.get().accept(e, true);
listenerRemover.get().run();
}
};
AWTEventListener listener = event -> {
if (event instanceof MouseEvent) {
int id = event.getID();
if (id != MouseEvent.MOUSE_CLICKED && id != MouseEvent.MOUSE_PRESSED)
return;
if (id == MouseEvent.MOUSE_CLICKED && !open.get()) {
listenerRemover.get().run();
((MouseEvent) event).consume();
return;
}
}
boolean doClose = event instanceof FocusEvent && !(DarkUIUtil.hasFocus(attachedComp, (FocusEvent) event) || DarkUIUtil.hasFocus(attachedComp) || DarkUIUtil.hasFocus(parent, (FocusEvent) event));
if (!doClose) {
Point p = MouseInfo.getPointerInfo().getLocation();
Point p2 = new Point(p);
Point p3 = new Point(p);
SwingUtilities.convertPointFromScreen(p2, parent);
SwingUtilities.convertPointFromScreen(p3, attachedComp);
doClose = !(parent.contains(p2) || attachedComp.contains(p3));
}
if (doClose) {
close.get().accept(event, true);
if (event instanceof InputEvent)
((InputEvent) event).consume();
}
};
close.set((e, runCloseAction) -> {
if (!open.get())
return;
popup.hide();
open.set(false);
if (onClose != null && runCloseAction)
onClose.run();
});
listenerRemover.set(() -> {
Toolkit.getDefaultToolkit().removeAWTEventListener(listener);
if (window != null)
window.removeComponentListener(windowListener);
afterClose.run();
});
KeyStroke escape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
attachedComp.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(escape, "closeEscape");
attachedComp.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(enter, "closeEnter");
attachedComp.getActionMap().put("closeEscape", Actions.create(e -> {
onAbort.run();
close.get().accept(e, false);
listenerRemover.get().run();
}));
attachedComp.getActionMap().put("closeEnter", Actions.create(e -> {
close.get().accept(e, true);
listenerRemover.get().run();
}));
SwingUtilities.invokeLater(() -> {
window.addComponentListener(windowListener);
Toolkit.getDefaultToolkit().addAWTEventListener(listener, AWTEvent.FOCUS_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK);
});
}
use of com.github.weisj.darklaf.components.tooltip.ToolTipContext in project darklaf by weisJ.
the class ToolTipUtil method getBestPositionMatch.
private static LocationResult getBestPositionMatch(final ToolTipContext context, final Point p) {
if (!context.isBestFit()) {
return new LocationResult(context.getToolTipLocation(p, null), true);
}
Alignment original = context.getAlignment();
Alignment originalCenter = context.getCenterAlignment();
LayoutConstraints layoutConstraints = calculateLayoutConstraints(context, p);
boolean isCenter = original == Alignment.CENTER;
Alignment targetAlignment = isCenter ? originalCenter : original;
if (context.isChooseBestInitialAlignment()) {
targetAlignment = probeAlignment(context, layoutConstraints);
}
boolean centerVertically = targetAlignment.isHorizontal();
boolean centerHorizontally = targetAlignment.isVertical();
Alignment[] alignments = getAlignments(targetAlignment);
Point pos;
BiConsumer<ToolTipContext, Alignment> setter = isCenter ? ToolTipContext::setCenterAlignment : ToolTipContext::setAlignment;
// Check if a position keeps the tooltip inside the window.
pos = tryAlignments(alignments, context, p, layoutConstraints, setter, centerHorizontally, centerVertically);
if (pos == null) {
// Try again with screen bounds instead.
layoutConstraints.windowBounds.setBounds(layoutConstraints.screenBoundary);
pos = tryAlignments(alignments, context, p, layoutConstraints, setter, centerHorizontally, centerVertically);
}
LocationResult result;
/*
* At this point if the tooltip is still extending outside the screen boundary we surrender and
* leave the tooltip as it was.
*/
if (pos == null) {
context.setAlignment(Alignment.CENTER);
context.setCenterAlignment(Alignment.CENTER);
result = new LocationResult(context.getFallBackPositionProvider().calculateFallbackPosition(context), !context.getFallBackPositionProvider().providesAbsolutePosition());
} else {
result = new LocationResult(pos, true);
}
context.updateToolTip();
context.setAlignment(original);
context.setCenterAlignment(originalCenter);
return result;
}
use of com.github.weisj.darklaf.components.tooltip.ToolTipContext in project darklaf by weisJ.
the class ToolTipUtil method applyContext.
public static void applyContext(final JToolTip toolTip) {
JComponent target = toolTip.getComponent();
if (target == null)
return;
ToolTipContext context = getToolTipContext(toolTip);
if (context == null)
return;
context.setTarget(target);
context.setToolTip(toolTip);
Point p = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(p, target);
LocationResult pos = getBestPositionMatch(context, p);
if (pos.point != null) {
moveToolTip(toolTip, pos, target);
}
}
Aggregations