diff --git a/window-menu-applet/window-menu-applet.vala b/window-menu-applet/window-menu-applet.vala index 4c834e1..dae2dfa 100644 --- a/window-menu-applet/window-menu-applet.vala +++ b/window-menu-applet/window-menu-applet.vala @@ -2,121 +2,120 @@ using WindowWidgets; namespace WindowMenuApplet{ - WindowWidgets.WindowMenuButton button; - Wnck.Window *window; - Wnck.Window *active_window; + class WindowMenu { + public WindowWidgets.WindowMenuButton button; + private Wnck.Window *window; + private Wnck.Window *active_window; - GLib.Settings gsettings; + public GLib.Settings gsettings = new GLib.Settings("org.mate.window-applets.window-menu"); - public void reload(){ - - // Disconnect signals from old window - if(window != null){ - window->icon_changed.disconnect(button.icon_set); - window->actions_changed.disconnect(button.menu_set); - window->state_changed.disconnect(reload); + public WindowMenu(){ + button = new WindowMenuButton(); } - if(active_window != null) - active_window->state_changed.disconnect(reload); + public void reload(){ + // Disconnect signals from old window + if(window != null){ + window->icon_changed.disconnect(button.icon_set); + window->actions_changed.disconnect(button.menu_set); + window->state_changed.disconnect(reload); + } - window = get_current_window(); + if(active_window != null) + active_window->state_changed.disconnect(reload); - button.window = window; - button.icon_set(); - button.menu_set(); + window = get_current_window(); - // Watch for changes to new controlled window - if(window != null){ - window->icon_changed.connect(button.icon_set); - window->actions_changed.connect(button.menu_set); - window->state_changed.connect(reload); - } + button.window = window; - // When active window is not the controlled window (because it is unmaximized), - // we need to watch its state as well - active_window = Wnck.Screen.get_default().get_active_window(); - if(active_window != null && active_window != window) - active_window->state_changed.connect(reload); - } + button.icon_set(); + button.menu_set(); - public void change_orient(MatePanel.Applet applet){ - MatePanel.AppletOrient orient = applet.get_orient(); - switch(orient){ - case MatePanel.AppletOrient.UP: - button.set_direction(Gtk.ArrowType.DOWN); - break; - case MatePanel.AppletOrient.DOWN: - button.set_direction(Gtk.ArrowType.UP); - break; - case MatePanel.AppletOrient.LEFT: - button.set_direction(Gtk.ArrowType.RIGHT); - break; - case MatePanel.AppletOrient.RIGHT: - button.set_direction(Gtk.ArrowType.LEFT); - break; - default: - break; + // Watch for changes to new controlled window + if(window != null){ + window->icon_changed.connect(button.icon_set); + window->actions_changed.connect(button.menu_set); + window->state_changed.connect(reload); + } + + // When active window is not the controlled window (because it is unmaximized), + // we need to watch its state as well + active_window = Wnck.Screen.get_default().get_active_window(); + if(active_window != null && active_window != window) + active_window->state_changed.connect(reload); } - reload(); + public void change_orient(MatePanel.Applet applet){ + MatePanel.AppletOrient orient = applet.get_orient(); + switch(orient){ + case MatePanel.AppletOrient.UP: + button.set_direction(Gtk.ArrowType.DOWN); + break; + case MatePanel.AppletOrient.DOWN: + button.set_direction(Gtk.ArrowType.UP); + break; + case MatePanel.AppletOrient.LEFT: + button.set_direction(Gtk.ArrowType.RIGHT); + break; + case MatePanel.AppletOrient.RIGHT: + button.set_direction(Gtk.ArrowType.LEFT); + break; + default: + break; + } - } + } - public void change_behaviour(){ - string behaviour = gsettings.get_string("behaviour"); + public void change_behaviour(){ + string behaviour = gsettings.get_string("behaviour"); - Wnck.Screen.get_default().window_closed.disconnect( reload ); + Wnck.Screen.get_default().window_closed.disconnect( reload ); - if(behaviour == "topmost-maximized") - Wnck.Screen.get_default().window_closed.connect( reload ); - } + if(behaviour == "topmost-maximized") + Wnck.Screen.get_default().window_closed.connect( reload ); + } - private Wnck.Window get_current_window(){ - Wnck.Window* win = null; - Wnck.WindowType window_type; - string behaviour = gsettings.get_string("behaviour"); - - switch(behaviour){ - case "active-always": - win = Wnck.Screen.get_default().get_active_window(); - if(win != null){ - window_type = win->get_window_type(); - if(window_type == Wnck.WindowType.DESKTOP || window_type == Wnck.WindowType.DOCK) + private Wnck.Window get_current_window(){ + Wnck.Window* win = null; + Wnck.WindowType window_type; + string behaviour = gsettings.get_string("behaviour"); + + switch(behaviour){ + case "active-always": + win = Wnck.Screen.get_default().get_active_window(); + if(win != null){ + window_type = win->get_window_type(); + if(window_type == Wnck.WindowType.DESKTOP || window_type == Wnck.WindowType.DOCK) + win = null; + } + break; + case "active-maximized": + win = Wnck.Screen.get_default().get_active_window(); + if(win != null && !win->is_maximized()) win = null; - } - break; - case "active-maximized": - win = Wnck.Screen.get_default().get_active_window(); - if(win != null && !win->is_maximized()) - win = null; - break; - case "topmost-maximized": - List windows = Wnck.Screen.get_default().get_windows_stacked().copy(); - windows.reverse(); - foreach(Wnck.Window* w in windows) { - if(w->is_maximized() && !w->is_minimized()){ - win = w; - break; + break; + case "topmost-maximized": + List windows = Wnck.Screen.get_default().get_windows_stacked().copy(); + windows.reverse(); + foreach(Wnck.Window* w in windows) { + if(w->is_maximized() && !w->is_minimized()){ + win = w; + break; + } } - } - break; - } + break; + } - return win; + return win; + } } private bool factory(MatePanel.Applet applet,string iid){ if(iid != "WindowMenuApplet")return false; - gsettings = new GLib.Settings("org.mate.window-applets.window-menu"); - - button = new WindowMenuButton(); - - change_behaviour(); - change_orient(applet); + var windowMenu = new WindowMenu(); Gtk.Builder builder = new Gtk.Builder(); @@ -146,10 +145,10 @@ namespace WindowMenuApplet{ string menu = """"""; menu += """"""; - gsettings.bind("behaviour",builder.get_object("behaviour"),"active_id",SettingsBindFlags.DEFAULT); - gsettings.changed["behaviour"].connect( () => { change_behaviour(); reload(); } ); + windowMenu.gsettings.bind("behaviour",builder.get_object("behaviour"),"active_id",SettingsBindFlags.DEFAULT); + windowMenu.gsettings.changed["behaviour"].connect( () => { windowMenu.change_behaviour(); windowMenu.reload(); } ); - applet.add(button); + applet.add(windowMenu.button); applet.setup_menu(menu,action_group); settings.delete_event.connect( (event) => { settings.hide() ; return true ; } ); @@ -158,11 +157,15 @@ namespace WindowMenuApplet{ settings_action.activate.connect( () => { settings.present() ; } ); about_action.activate.connect( () => { about.present() ; } ); - applet.change_orient.connect( () => { change_orient(applet) ; } ); + applet.change_orient.connect( () => { windowMenu.change_orient(applet); windowMenu.reload(); } ); applet.show_all(); - Wnck.Screen.get_default().active_window_changed.connect( reload ); + Wnck.Screen.get_default().active_window_changed.connect( windowMenu.reload ); + + windowMenu.change_orient(applet); + windowMenu.change_behaviour(); + windowMenu.reload(); return true; }