Skip to content

Commit

Permalink
[window-menu] Convert to class to allow multiple instances
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielfin authored and flexiondotorg committed Mar 4, 2021
1 parent d072b89 commit 5bc45f7
Showing 1 changed file with 97 additions and 94 deletions.
191 changes: 97 additions & 94 deletions window-menu-applet/window-menu-applet.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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<weak Wnck.Window> 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<weak Wnck.Window> 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();

Expand Down Expand Up @@ -146,10 +145,10 @@ namespace WindowMenuApplet{
string menu = """<menuitem name="Settings" action="settings" />""";
menu += """<menuitem name="About" action="about" />""";

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 ; } );
Expand All @@ -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;
}
Expand Down

0 comments on commit 5bc45f7

Please sign in to comment.