Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show Tilda on mouse cursor monitor #253

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/configsys.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ static cfg_opt_t config_opts[] = {
CFG_BOOL("confirm_close_tab", FALSE, CFGF_NONE),

CFG_INT("back_alpha", 0xffff, CFGF_NONE),
CFG_BOOL("show_on_mouse_monitor", FALSE, CFGF_NONE),

/* Whether to show the full tab title as a tooltip */
CFG_BOOL("show_title_tooltip", FALSE, CFGF_NONE),
Expand Down
9 changes: 8 additions & 1 deletion src/key_grabber.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <string.h>

#include <gdk/gdkx.h>
#include <gdk/gdk.h>

#define ANIMATION_UP 0
#define ANIMATION_DOWN 1
Expand Down Expand Up @@ -149,13 +150,19 @@ void tilda_window_set_active (tilda_window *tw)
DEBUG_ASSERT (tw != NULL);

GdkScreen *screen = gtk_widget_get_screen (tw->window);

gboolean show_on_nondefault_screen =
tilda_window_move_to_mouse_monitor(tw, screen);
if (!show_on_nondefault_screen) {
gtk_window_move (GTK_WINDOW(tw->window), config_getint ("x_pos"), config_getint ("y_pos"));
}

Display *x11_display = GDK_WINDOW_XDISPLAY (gdk_screen_get_root_window (screen));
Window x11_window = GDK_WINDOW_XID (gtk_widget_get_window (tw->window) );
Window x11_root_window = GDK_WINDOW_XID ( gdk_screen_get_root_window (screen) );

XEvent event;
long mask = SubstructureRedirectMask | SubstructureNotifyMask;
gtk_window_move (GTK_WINDOW(tw->window), config_getint ("x_pos"), config_getint ("y_pos"));
if (gdk_x11_screen_supports_net_wm_hint (screen,
gdk_atom_intern_static_string ("_NET_ACTIVE_WINDOW")))
{
Expand Down
20 changes: 10 additions & 10 deletions src/tilda.ui
Original file line number Diff line number Diff line change
Expand Up @@ -1635,24 +1635,24 @@
</packing>
</child>
<child>
<object class="GtkLabel" id="label30">
<object class="GtkCheckButton" id="check_show_on_mouse_monitor">
<property name="label" translatable="yes">Show on mouse monitor</property>
<property name="use_action_appearance">True</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="receives_default">False</property>
<property name="xalign">0</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
<signal name="toggled" handler="check_show_on_mouse_monitor_toggled_cb" swapped="no"/>
</object>
<packing>
<property name="left_attach">3</property>
<property name="left_attach">2</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label31">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">0</property>
</packing>
<placeholder/>
</child>
</object>
</child>
Expand Down
44 changes: 44 additions & 0 deletions src/tilda_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -1169,3 +1169,47 @@ gint tilda_window_confirm_quit (tilda_window *tw)

return GDK_EVENT_STOP;
}

/* Get the index of the monitor the mouse cursor is currently on */
gint get_monitor_for_cursor (tilda_window *tw, GdkScreen *screen)
{
gint mouse_x, mouse_y;
GdkDisplay *disp = gdk_display_get_default ();
GdkDeviceManager *device_manager = gdk_display_get_device_manager (disp);
GdkDevice *device = gdk_device_manager_get_client_pointer (device_manager);

gdk_device_get_position (device, &screen, &mouse_x, &mouse_y);
return gdk_screen_get_monitor_at_point (screen, mouse_x, mouse_y);
}

gboolean tilda_window_move_to_mouse_monitor (tilda_window *tw, GdkScreen *screen)
{
if (config_getbool("show_on_mouse_monitor")) {
gint mouse_monitor = get_monitor_for_cursor(tw, screen);

/* Get the monitor number on which the window is currently shown
* Idea: get the configured window position relatively to the
* configured monitor on which Tilda usually appears, and apply the
* same position on the monitor the mouse cursor is currently on */
gint config_monitor = find_monitor_number (tw);
if (config_monitor != mouse_monitor) {
gint window_x, window_y;
GdkRectangle* config_monitor_rect = malloc (sizeof (GdkRectangle));
GdkRectangle* mouse_monitor_rect = malloc (sizeof (GdkRectangle));;
gdk_screen_get_monitor_workarea (screen, config_monitor, config_monitor_rect);
gdk_screen_get_monitor_workarea (screen, mouse_monitor, mouse_monitor_rect);

gint x_offset,y_offset;
x_offset = config_getint ("x_pos") - config_monitor_rect->x;
y_offset = config_getint ("y_pos") - config_monitor_rect->y;
window_x = mouse_monitor_rect->x + x_offset;
window_y = mouse_monitor_rect->y + y_offset;

free (config_monitor_rect);
free (mouse_monitor_rect);
gtk_window_move (GTK_WINDOW(tw->window), window_x, window_y);
return TRUE;
}
}
return FALSE;
}
5 changes: 5 additions & 0 deletions src/tilda_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ void tilda_window_toggle_searchbar (tilda_window *tw);
*/
gint tilda_window_confirm_quit (tilda_window *tw);

/**
* Move the tilda window to the monitor the mouse cursor is located on.
*/
gboolean tilda_window_move_to_mouse_monitor (tilda_window *tw, GdkScreen *screen);

#define TILDA_WINDOW(data) ((tilda_window *)(data))

/* Allow scales a bit smaller and a bit larger than the usual pango ranges */
Expand Down
14 changes: 13 additions & 1 deletion src/wizard.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ static void update_palette_color_button(gint idx);
static int find_centering_coordinate (tilda_window *tw, enum dimensions dimension);
static void initialize_geometry_spinners(tilda_window *tw);

static gint find_monitor_number(tilda_window *tw)
gint find_monitor_number(tilda_window *tw)
{
DEBUG_FUNCTION ("find_monitor_number");

Expand Down Expand Up @@ -1002,6 +1002,15 @@ static void entry_word_chars_changed (GtkWidget *w, tilda_window *tw)
}
}

static void check_show_on_mouse_monitor_toggled_cb(GtkWidget *w, tilda_window *tw) {
const gboolean show_on_mouse_monitor = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(w));
const GtkWidget *combo_choose_monitor =
GTK_WIDGET (gtk_builder_get_object (xml, "combo_choose_monitor"));

config_setbool ("show_on_mouse_monitor", show_on_mouse_monitor);
gtk_widget_set_sensitive (GTK_WIDGET(combo_choose_monitor), !show_on_mouse_monitor);
}

/*
* Finds the coordinate that will center the tilda window in the screen.
*
Expand Down Expand Up @@ -1940,6 +1949,7 @@ static void set_wizard_state_from_config (tilda_window *tw) {


initialize_geometry_spinners(tw);
CHECK_BUTTON ("check_show_on_mouse_monitor", "show_on_mouse_monitor");
CHECK_BUTTON ("check_enable_transparency", "enable_transparency");
CHECK_BUTTON ("check_animated_pulldown", "animation");
SPIN_BUTTON ("spin_animation_delay", "slide_sleep_usec");
Expand All @@ -1950,6 +1960,7 @@ static void set_wizard_state_from_config (tilda_window *tw) {
CHECK_BUTTON ("check_show_single_tab", "show_single_tab");
CHECK_BUTTON ("check_show_title_tooltip", "show_title_tooltip");

SET_SENSITIVE_BY_CONFIG_NBOOL ("combo_choose_monitor", "show_on_mouse_monitor");
SET_SENSITIVE_BY_CONFIG_BOOL ("label_level_of_transparency","enable_transparency");
SET_SENSITIVE_BY_CONFIG_BOOL ("spin_level_of_transparency","enable_transparency");
SET_SENSITIVE_BY_CONFIG_BOOL ("label_animation_delay","animation");
Expand Down Expand Up @@ -2087,6 +2098,7 @@ static void connect_wizard_signals (TildaWizard *wizard)

/* Appearance Tab */
CONNECT_SIGNAL ("combo_choose_monitor", "changed", combo_monitor_selection_changed_cb, tw);
CONNECT_SIGNAL ("check_show_on_mouse_monitor", "toggled", check_show_on_mouse_monitor_toggled_cb, tw);
CONNECT_SIGNAL ("spin_height_percentage","value-changed",spin_height_percentage_value_changed_cb, tw);
CONNECT_SIGNAL ("spin_height_pixels","value-changed",spin_height_pixels_value_changed_cb, tw);
CONNECT_SIGNAL ("spin_width_percentage","value-changed",spin_width_percentage_value_changed_cb, tw);
Expand Down