From 7d884242bd2c022f1104af309f35f5bf55d38068 Mon Sep 17 00:00:00 2001 From: Sanel Zukan Date: Wed, 11 Apr 2012 16:15:01 +0000 Subject: [PATCH] Do not run timeout callbacks when panel is hidden. This will prevent reading cpu or memory status when their widgets are not visible. --- ede-panel/Panel.cpp | 49 ++++++++++++++------ ede-panel/Panel.h | 13 +++++- ede-panel/applets/Jamfile | 1 + ede-panel/applets/clock/Clock.cpp | 11 +++-- ede-panel/applets/cpu-monitor/CpuMonitor.cpp | 17 ++++++- ede-panel/applets/cpu-monitor/CpuMonitor.h | 1 + ede-panel/applets/mem-monitor/MemMonitor.cpp | 19 +++++++- ede-panel/applets/mem-monitor/MemMonitor.h | 1 + 8 files changed, 90 insertions(+), 22 deletions(-) diff --git a/ede-panel/Panel.cpp b/ede-panel/Panel.cpp index 66c6ba0..a3f4bee 100644 --- a/ede-panel/Panel.cpp +++ b/ede-panel/Panel.cpp @@ -27,15 +27,16 @@ #undef MAX #define MAX(x,y) ((x) > (y) ? (x) : (y)) -EDELIB_NS_USING(list) -EDELIB_NS_USING(Resource) -EDELIB_NS_USING(String) -EDELIB_NS_USING(window_xid_create) -EDELIB_NS_USING(build_filename) -EDELIB_NS_USING(netwm_window_set_strut) -EDELIB_NS_USING(netwm_window_set_type) -EDELIB_NS_USING(netwm_workarea_get_size) -EDELIB_NS_USING(NETWM_WINDOW_TYPE_DOCK) +EDELIB_NS_USING_LIST(10, (list, + Resource, + String, + window_xid_create, + build_filename, + netwm_window_set_strut, + netwm_window_remove_strut, + netwm_window_set_type, + netwm_workarea_get_size, + NETWM_WINDOW_TYPE_DOCK)) typedef list WidgetList; typedef list::iterator WidgetListIt; @@ -180,6 +181,7 @@ Panel::Panel() : PanelWindow(300, 30) { screen_x = screen_y = screen_w = screen_h = screen_h_half = 0; width_perc = 100; can_move_widgets = false; + can_drag = true; box(FL_UP_BOX); read_config(); @@ -330,7 +332,6 @@ void Panel::show(void) { if(!netwm_workarea_get_size(X, Y, W, H)) Fl::screen_xywh(X, Y, W, H); - /* TODO: these numbers are also used to calculate panel x,y,w,h so rename it! */ screen_x = X; screen_y = Y; screen_w = W; @@ -340,7 +341,7 @@ void Panel::show(void) { /* calculate panel percentage width if given */ if(width_perc < 100) { W = (width_perc * screen_w) / 100; - screen_x = (screen_w / 2) - (W / 2); + X = (screen_w / 2) - (W / 2); } /* set size as soon as possible, since do_layout() depends on it */ @@ -351,12 +352,12 @@ void Panel::show(void) { /* position it, this is done after XID was created */ if(vpos == PANEL_POSITION_BOTTOM) { - position(screen_x, screen_h - h()); + position(X, screen_h - h()); if(width_perc >= 100) netwm_window_set_strut(fl_xid(this), 0, 0, 0, h()); } else { /* FIXME: this does not work well with edewm (nor pekwm). kwin do it correctly. */ - position(screen_x, screen_y); + position(X, Y); if(width_perc >= 100) netwm_window_set_strut(fl_xid(this), 0, 0, h(), 0); } @@ -382,19 +383,21 @@ int Panel::handle(int e) { return 1; case FL_DRAG: { + if(!can_drag) return 1; + /* are moving the panel; only vertical moving is supported */ cursor(FL_CURSOR_MOVE); /* snap it to the top or bottom, depending on pressed mouse location */ if(Fl::event_y_root() <= screen_h_half && y() > screen_h_half) { - position(screen_x, screen_y); + position(x(), screen_y); if(width_perc >= 100) netwm_window_set_strut(fl_xid(this), 0, 0, h(), 0); vpos = PANEL_POSITION_TOP; } if(Fl::event_y_root() > screen_h_half && y() < screen_h_half) { - position(screen_x, screen_h - h()); + position(x(), screen_h - h()); if(width_perc >= 100) netwm_window_set_strut(fl_xid(this), 0, 0, 0, h()); vpos = PANEL_POSITION_BOTTOM; @@ -430,6 +433,7 @@ void Panel::load_applets(void) { "start_menu.so", "quick_launch.so", "pager.so", + "hider.so", "clock.so", "taskbar.so", "keyboard_layout.so", @@ -456,6 +460,7 @@ void Panel::load_applets(void) { mgr.load("./applets/start-menu/start_menu.so"); mgr.load("./applets/quick-launch/quick_launch.so"); mgr.load("./applets/pager/pager.so"); + mgr.load("./applets/hider/hider.so"); mgr.load("./applets/clock/clock.so"); mgr.load("./applets/taskbar/taskbar.so"); mgr.load("./applets/keyboard-layout/keyboard_layout.so"); @@ -465,3 +470,17 @@ void Panel::load_applets(void) { mgr.fill_group(this); #endif } + +/* TODO: can be better */ +void Panel::apply_struts(bool apply) { + if(!(width_perc >= 100)) return; + + int bottom = 0, top = 0; + + if(vpos == PANEL_POSITION_TOP) + top = !apply ? 1 : h(); + else + bottom = !apply ? 1 : h(); + + netwm_window_set_strut(fl_xid(this), 0, 0, top, bottom); +} diff --git a/ede-panel/Panel.h b/ede-panel/Panel.h index 5c8004d..8cbe314 100644 --- a/ede-panel/Panel.h +++ b/ede-panel/Panel.h @@ -21,7 +21,7 @@ private: int sx, sy; int screen_x, screen_y, screen_w, screen_h, screen_h_half; int width_perc; /* user specified panel width */ - bool can_move_widgets; + bool can_move_widgets, can_drag; AppletManager mgr; @@ -40,6 +40,17 @@ public: int panel_w(void) { return w(); } int panel_h(void) { return h(); } + void screen_size(int &x, int &y, int &w, int &h) { + x = screen_x; + y = screen_y; + w = screen_w; + h = screen_h; + } + + /* so applets can do something */ + void apply_struts(bool apply); + void allow_drag(bool d) { can_drag = d; } + void relayout(void) { do_layout(); } }; diff --git a/ede-panel/applets/Jamfile b/ede-panel/applets/Jamfile index 07cdb8f..6d8dbe2 100644 --- a/ede-panel/applets/Jamfile +++ b/ede-panel/applets/Jamfile @@ -13,6 +13,7 @@ SubDir TOP ede-panel applets ; # SubInclude TOP ede-panel applets demo ; SubInclude TOP ede-panel applets clock ; SubInclude TOP ede-panel applets cpu-monitor ; +SubInclude TOP ede-panel applets hider ; SubInclude TOP ede-panel applets mem-monitor ; SubInclude TOP ede-panel applets keyboard-layout ; SubInclude TOP ede-panel applets pager ; diff --git a/ede-panel/applets/clock/Clock.cpp b/ede-panel/applets/clock/Clock.cpp index f0f8d8a..aaf9a81 100644 --- a/ede-panel/applets/clock/Clock.cpp +++ b/ede-panel/applets/clock/Clock.cpp @@ -34,7 +34,6 @@ public: static void clock_refresh(void *o) { Clock *c = (Clock *)o; c->update_time(); - Fl::repeat_timeout(1.0, clock_refresh, o); } @@ -57,13 +56,19 @@ void Clock::update_time(void) { int Clock::handle(int e) { switch(e) { - case FL_SHOW: + case FL_SHOW: { + int ret = Fl_Box::handle(e); Fl::add_timeout(0, clock_refresh, this); - break; + return ret; + } case FL_RELEASE: run_async("ede-timedate"); break; + + case FL_HIDE: + Fl::remove_timeout(clock_refresh); + /* fallthrough */ } return Fl_Box::handle(e); diff --git a/ede-panel/applets/cpu-monitor/CpuMonitor.cpp b/ede-panel/applets/cpu-monitor/CpuMonitor.cpp index d42c364..f0f0458 100644 --- a/ede-panel/applets/cpu-monitor/CpuMonitor.cpp +++ b/ede-panel/applets/cpu-monitor/CpuMonitor.cpp @@ -142,7 +142,6 @@ CPUMonitor::CPUMonitor() : Fl_Box(0, 0, 45, 25) colors[IWM_IDLE] = FL_BACKGROUND_COLOR; layout(); - Fl::add_timeout(UPDATE_INTERVAL, cpu_timeout_cb, this); } void CPUMonitor::clear() @@ -247,6 +246,22 @@ void CPUMonitor::layout() { } } +int CPUMonitor::handle(int e) { + switch(e) { + case FL_SHOW: { + int ret = Fl_Box::handle(e); + Fl::add_timeout(UPDATE_INTERVAL, cpu_timeout_cb, this); + return ret; + } + + case FL_HIDE: + Fl::remove_timeout(cpu_timeout_cb); + /* fallthrough */ + } + + return Fl_Box::handle(e); +} + void CPUMonitor::update_status() { if(!cpu) return; diff --git a/ede-panel/applets/cpu-monitor/CpuMonitor.h b/ede-panel/applets/cpu-monitor/CpuMonitor.h index 8d4f788..0db90e5 100644 --- a/ede-panel/applets/cpu-monitor/CpuMonitor.h +++ b/ede-panel/applets/cpu-monitor/CpuMonitor.h @@ -39,6 +39,7 @@ public: void draw(); void layout(); + int handle(int e); int samples() const { return m_samples; } }; diff --git a/ede-panel/applets/mem-monitor/MemMonitor.cpp b/ede-panel/applets/mem-monitor/MemMonitor.cpp index 31fbf9e..a5e9eee 100644 --- a/ede-panel/applets/mem-monitor/MemMonitor.cpp +++ b/ede-panel/applets/mem-monitor/MemMonitor.cpp @@ -10,7 +10,7 @@ EDELIB_NS_USING(color_rgb_to_fltk) -#define UPDATE_INTERVAL .5f +#define UPDATE_INTERVAL 1.0f #define STR_CMP(first, second, n) (strncmp(first, second, n) == 0) static int height_from_perc(int perc, int h) { @@ -36,7 +36,6 @@ static void mem_timeout_cb(void *d) { MemMonitor::MemMonitor() : Fl_Box(0, 0, 45, 25), mem_usedp(0), swap_usedp(0) { box(FL_THIN_DOWN_BOX); - Fl::add_timeout(UPDATE_INTERVAL, mem_timeout_cb, this); } void MemMonitor::update_status(void) { @@ -87,6 +86,22 @@ void MemMonitor::draw(void) { fl_rectf(X + W2, Y + H - sh, W2, sh, (Fl_Color)color_rgb_to_fltk(54, 136, 79)); } +int MemMonitor::handle(int e) { + switch(e) { + case FL_SHOW: { + int ret = Fl_Box::handle(e); + Fl::add_timeout(UPDATE_INTERVAL, mem_timeout_cb, this); + return ret; + } + + case FL_HIDE: + Fl::remove_timeout(mem_timeout_cb); + /* fallthrough */ + } + + return Fl_Box::handle(e); +} + EDE_PANEL_APPLET_EXPORT ( MemMonitor, EDE_PANEL_APPLET_OPTION_ALIGN_RIGHT, diff --git a/ede-panel/applets/mem-monitor/MemMonitor.h b/ede-panel/applets/mem-monitor/MemMonitor.h index 24f3346..63a9b2f 100644 --- a/ede-panel/applets/mem-monitor/MemMonitor.h +++ b/ede-panel/applets/mem-monitor/MemMonitor.h @@ -11,6 +11,7 @@ public: MemMonitor(); void update_status(void); void draw(void); + int handle(int e); }; #endif