开发者

gtk: how to hide a window when the application loses focus

I want to duplicate the behaviour of tool windows in OpenOfice. When the application loses focus, the tool windows (if they are not docked) are hidden.

So, I have a main window, and another utility window (win_dock). I want to hide win_dock when all the windows of the application loses focus and show it again if a window gain focus.

What I did is that I connected to the focus-in-event and focus-out-event of all windows of the application, and I maintain a counter of how many windows have focus. When this counter drops to zero, I want to hide win_dock, and if this counter is positive again, I want to show win_dock

The problem is with this solution I can never focus win_dock. Because when I click on it, the main window drops the focus, so it hides win_dock that still hadn't gained the focus. Nevertheless the focus-in-event is still sent to win_dock and the windows reappears. But in the meantime it has lost the focus.

Do you have a better solution?

Here i开发者_StackOverflow社区s the Vala source code:

public class Main
{

  private Gtk.Builder builder;
  private Gtk.Window  win_messages;
  private Gtk.Window  win_dock;
  private int focus_count = 0;

  public Main() {
    builder = new Gtk.Builder();
    builder.add_from_file("ui2.glade");
    win_messages = builder.get_object("win_messages") as Gtk.Window;
    win_dock     = builder.get_object("win_dock")     as Gtk.Window;
    handle_focus(win_messages);
    handle_focus(win_dock);
  }

  public void start(){
    win_messages.show_all();    
    //win_dock.show_all();
    Gtk.main();
  }

  private void handle_focus(Gtk.Window w) {
    w.focus_in_event.connect ((w, e) => {
      stdout.printf("Focus In  (%s)\n", w.name);
      focus_count++;
      manage_focus(w == win_dock);
    });
    w.focus_out_event.connect((w, e) => {
      stdout.printf("Focus Out (%s)\n", w.name);
      focus_count--;
      manage_focus(w == win_dock);
    });
  }

  private void manage_focus(bool is_dock){
    if(focus_count > 0) {
      win_dock.show_all();
      stdout.printf("Show (focus: %d)\n", focus_count);
    } else if(is_dock) {
      win_dock.hide_all();
      stdout.printf("Hide (focus: %d, has: %d) dock\n", focus_count, win_dock.is_active ? 1 : 0);
    } else if(!is_dock) {
      if(win_dock.is_active) {
        win_dock.hide_all();
        stdout.printf("Hide (focus: %d, has: %d) !dock\n", focus_count, win_dock.is_active ? 1 : 0);
      } else {
        stdout.printf("Nop  (focus: %d, has: %d) !dock\n", focus_count, win_dock.is_active ? 1 : 0);
      }
    }
  }

  public static int main (string[] args)
  {
    Gtk.init (ref args);
    Main m = new Main();
    m.start();

    return 0;
  }
}

Thanks.


Is there a good reason to make the dialog disappear? Wouldn't it be enough to make win_dock transient (win_dock.set_transient_for) for the main window?

Otherwise you could try using GLib.Idle.add to call manage_focuswhich will cause
manage_focus to run after all your focus event callbacks have run. It will then have the correct number of focused windows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜