WSWindowManager.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. #include "WSWindowManager.h"
  2. #include "WSWindow.h"
  3. #include "WSScreen.h"
  4. #include "WSMessageLoop.h"
  5. #include <SharedGraphics/Font.h>
  6. #include <SharedGraphics/Painter.h>
  7. #include <SharedGraphics/CharacterBitmap.h>
  8. #include <AK/StdLibExtras.h>
  9. #include <LibC/errno_numbers.h>
  10. #include "WSMenu.h"
  11. #include "WSMenuBar.h"
  12. #include "WSMenuItem.h"
  13. #include <WindowServer/WSClientConnection.h>
  14. #include <unistd.h>
  15. #include <stdio.h>
  16. #include <time.h>
  17. #ifdef KERNEL
  18. #include <Kernel/ProcFS.h>
  19. #endif
  20. //#define DEBUG_COUNTERS
  21. //#define DEBUG_WID_IN_TITLE_BAR
  22. #define RESIZE_DEBUG
  23. #define USE_WALLPAPER
  24. static const int window_titlebar_height = 16;
  25. static inline Rect menu_window_rect(const Rect& rect)
  26. {
  27. return rect.inflated(2, 2);
  28. }
  29. static inline Rect title_bar_rect(const Rect& window)
  30. {
  31. return {
  32. window.x() - 1,
  33. window.y() - window_titlebar_height,
  34. window.width() + 2,
  35. window_titlebar_height
  36. };
  37. }
  38. static inline Rect title_bar_text_rect(const Rect& window)
  39. {
  40. auto titlebar_rect = title_bar_rect(window);
  41. return {
  42. titlebar_rect.x() + 2,
  43. titlebar_rect.y(),
  44. titlebar_rect.width() - 4,
  45. titlebar_rect.height()
  46. };
  47. }
  48. static inline Rect close_button_rect_for_window(const Rect& window_rect)
  49. {
  50. auto titlebar_inner_rect = title_bar_text_rect(window_rect);
  51. int close_button_margin = 1;
  52. int close_button_size = titlebar_inner_rect.height() - close_button_margin * 2;
  53. return Rect {
  54. titlebar_inner_rect.right() - close_button_size + 1,
  55. titlebar_inner_rect.top() + close_button_margin,
  56. close_button_size,
  57. close_button_size - 1
  58. };
  59. }
  60. static inline Rect border_window_rect(const Rect& window)
  61. {
  62. auto titlebar_rect = title_bar_rect(window);
  63. return { titlebar_rect.x() - 1,
  64. titlebar_rect.y() - 1,
  65. titlebar_rect.width() + 2,
  66. window_titlebar_height + window.height() + 3
  67. };
  68. }
  69. static inline Rect outer_window_rect(const Rect& window)
  70. {
  71. auto rect = border_window_rect(window);
  72. rect.inflate(2, 2);
  73. return rect;
  74. }
  75. static WSWindowManager* s_the;
  76. WSWindowManager& WSWindowManager::the()
  77. {
  78. ASSERT(s_the);
  79. return *s_the;
  80. }
  81. static const char* cursor_bitmap_inner_ascii = {
  82. " # "
  83. " ## "
  84. " ### "
  85. " #### "
  86. " ##### "
  87. " ###### "
  88. " ####### "
  89. " ######## "
  90. " ######### "
  91. " ########## "
  92. " ###### "
  93. " ## ## "
  94. " # ## "
  95. " ## "
  96. " ## "
  97. " ## "
  98. " "
  99. };
  100. static const char* cursor_bitmap_outer_ascii = {
  101. "## "
  102. "# # "
  103. "# # "
  104. "# # "
  105. "# # "
  106. "# # "
  107. "# # "
  108. "# # "
  109. "# # "
  110. "# # "
  111. "# #### "
  112. "# ## # "
  113. "# # # # "
  114. "## # # "
  115. " # # "
  116. " # # "
  117. " ## "
  118. };
  119. void WSWindowManager::flip_buffers()
  120. {
  121. swap(m_front_bitmap, m_back_bitmap);
  122. swap(m_front_painter, m_back_painter);
  123. int new_y_offset = m_buffers_are_flipped ? 0 : m_screen_rect.height();
  124. WSScreen::the().set_y_offset(new_y_offset);
  125. m_buffers_are_flipped = !m_buffers_are_flipped;
  126. }
  127. WSWindowManager::WSWindowManager()
  128. : m_screen(WSScreen::the())
  129. , m_screen_rect(m_screen.rect())
  130. , m_flash_flush(false)
  131. {
  132. s_the = this;
  133. #ifndef DEBUG_COUNTERS
  134. (void)m_compose_count;
  135. (void)m_flush_count;
  136. #endif
  137. auto size = m_screen_rect.size();
  138. m_front_bitmap = GraphicsBitmap::create_wrapper(GraphicsBitmap::Format::RGB32, size, m_screen.scanline(0));
  139. m_back_bitmap = GraphicsBitmap::create_wrapper(GraphicsBitmap::Format::RGB32, size, m_screen.scanline(size.height()));
  140. m_front_painter = make<Painter>(*m_front_bitmap);
  141. m_back_painter = make<Painter>(*m_back_bitmap);
  142. m_font = Font::default_font();
  143. m_front_painter->set_font(font());
  144. m_back_painter->set_font(font());
  145. m_background_color = Color(50, 50, 50);
  146. m_active_window_border_color = Color(110, 34, 9);
  147. m_active_window_border_color2 = Color(244, 202, 158);
  148. m_active_window_title_color = Color::White;
  149. m_inactive_window_border_color = Color(128, 128, 128);
  150. m_inactive_window_border_color2 = Color(192, 192, 192);
  151. m_inactive_window_title_color = Color(213, 208, 199);
  152. m_dragging_window_border_color = Color(161, 50, 13);
  153. m_dragging_window_border_color2 = Color(250, 220, 187);
  154. m_dragging_window_title_color = Color::White;
  155. m_cursor_bitmap_inner = CharacterBitmap::create_from_ascii(cursor_bitmap_inner_ascii, 12, 17);
  156. m_cursor_bitmap_outer = CharacterBitmap::create_from_ascii(cursor_bitmap_outer_ascii, 12, 17);
  157. #ifdef USE_WALLPAPER
  158. m_wallpaper_path = "/res/wallpapers/cool.rgb";
  159. m_wallpaper = GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, m_wallpaper_path, { 1024, 768 });
  160. #endif
  161. #ifdef KERNEL
  162. ProcFS::the().add_sys_bool("wm_flash_flush", m_flash_flush);
  163. ProcFS::the().add_sys_string("wm_wallpaper", m_wallpaper_path, [this] {
  164. m_wallpaper = GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, m_wallpaper_path, m_screen_rect.size());
  165. invalidate(m_screen_rect);
  166. });
  167. #endif
  168. m_menu_selection_color = Color::from_rgb(0x84351a);
  169. {
  170. byte system_menu_name[] = { 0xf8, 0 };
  171. m_system_menu = make<WSMenu>(nullptr, -1, String((const char*)system_menu_name));
  172. m_system_menu->add_item(make<WSMenuItem>(0, "Launch Terminal"));
  173. m_system_menu->add_item(make<WSMenuItem>(WSMenuItem::Separator));
  174. m_system_menu->add_item(make<WSMenuItem>(1, "640x480"));
  175. m_system_menu->add_item(make<WSMenuItem>(2, "800x600"));
  176. m_system_menu->add_item(make<WSMenuItem>(3, "1024x768"));
  177. m_system_menu->add_item(make<WSMenuItem>(WSMenuItem::Separator));
  178. m_system_menu->add_item(make<WSMenuItem>(4, "About..."));
  179. m_system_menu->on_item_activation = [this] (WSMenuItem& item) {
  180. if (item.identifier() == 0) {
  181. if (fork() == 0) {
  182. execl("/bin/Terminal", "/bin/Terminal", nullptr);
  183. ASSERT_NOT_REACHED();
  184. }
  185. return;
  186. }
  187. switch (item.identifier()) {
  188. case 1: set_resolution(640, 480); break;
  189. case 2: set_resolution(800, 600); break;
  190. case 3: set_resolution(1024, 768); break;
  191. }
  192. if (item.identifier() == 4) {
  193. if (fork() == 0) {
  194. execl("/bin/About", "/bin/About", nullptr);
  195. ASSERT_NOT_REACHED();
  196. }
  197. return;
  198. }
  199. dbgprintf("WSMenu 1 item activated: '%s'\n", item.text().characters());
  200. };
  201. }
  202. // NOTE: This ensures that the system menu has the correct dimensions.
  203. set_current_menubar(nullptr);
  204. WSMessageLoop::the().start_timer(300, [this] {
  205. static time_t last_update_time;
  206. time_t now = time(nullptr);
  207. if (now != last_update_time) {
  208. invalidate(menubar_rect());
  209. last_update_time = now;
  210. }
  211. });
  212. invalidate();
  213. compose();
  214. }
  215. WSWindowManager::~WSWindowManager()
  216. {
  217. }
  218. void WSWindowManager::set_resolution(int width, int height)
  219. {
  220. if (m_screen_rect.width() == width && m_screen_rect.height() == height)
  221. return;
  222. m_screen.set_resolution(width, height);
  223. m_screen_rect = m_screen.rect();
  224. m_front_bitmap = GraphicsBitmap::create_wrapper(GraphicsBitmap::Format::RGB32, { width, height }, m_screen.scanline(0));
  225. m_back_bitmap = GraphicsBitmap::create_wrapper(GraphicsBitmap::Format::RGB32, { width, height }, m_screen.scanline(height));
  226. m_front_painter = make<Painter>(*m_front_bitmap);
  227. m_back_painter = make<Painter>(*m_back_bitmap);
  228. m_buffers_are_flipped = false;
  229. invalidate();
  230. compose();
  231. }
  232. template<typename Callback>
  233. void WSWindowManager::for_each_active_menubar_menu(Callback callback)
  234. {
  235. callback(*m_system_menu);
  236. if (m_current_menubar)
  237. m_current_menubar->for_each_menu(callback);
  238. }
  239. int WSWindowManager::menubar_menu_margin() const
  240. {
  241. return 16;
  242. }
  243. void WSWindowManager::set_current_menubar(WSMenuBar* menubar)
  244. {
  245. if (menubar)
  246. m_current_menubar = menubar->make_weak_ptr();
  247. else
  248. m_current_menubar = nullptr;
  249. dbgprintf("[WM] Current menubar is now %p\n", menubar);
  250. Point next_menu_location { menubar_menu_margin() / 2, 3 };
  251. for_each_active_menubar_menu([&] (WSMenu& menu) {
  252. int text_width = font().width(menu.name());
  253. menu.set_rect_in_menubar({ next_menu_location.x() - menubar_menu_margin() / 2, 0, text_width + menubar_menu_margin(), menubar_rect().height() - 1 });
  254. menu.set_text_rect_in_menubar({ next_menu_location, { text_width, font().glyph_height() } });
  255. next_menu_location.move_by(menu.rect_in_menubar().width(), 0);
  256. return true;
  257. });
  258. invalidate(menubar_rect());
  259. }
  260. static const char* s_close_button_bitmap_data = {
  261. "## ##"
  262. "### ###"
  263. " ###### "
  264. " #### "
  265. " ## "
  266. " #### "
  267. " ###### "
  268. "### ###"
  269. "## ##"
  270. };
  271. static CharacterBitmap* s_close_button_bitmap;
  272. static const int s_close_button_bitmap_width = 8;
  273. static const int s_close_button_bitmap_height = 9;
  274. void WSWindowManager::paint_window_frame(WSWindow& window)
  275. {
  276. //printf("[WM] paint_window_frame {%p}, rect: %d,%d %dx%d\n", &window, window.rect().x(), window.rect().y(), window.rect().width(), window.rect().height());
  277. if (window.type() == WSWindowType::Menu) {
  278. m_back_painter->draw_rect(menu_window_rect(window.rect()), Color::LightGray);
  279. return;
  280. }
  281. auto titlebar_rect = title_bar_rect(window.rect());
  282. auto titlebar_inner_rect = title_bar_text_rect(window.rect());
  283. auto outer_rect = outer_window_rect(window.rect());
  284. auto border_rect = border_window_rect(window.rect());
  285. auto close_button_rect = close_button_rect_for_window(window.rect());
  286. auto titlebar_title_rect = titlebar_inner_rect;
  287. titlebar_title_rect.set_width(font().glyph_width() * window.title().length());
  288. Rect inner_border_rect {
  289. window.x() - 1,
  290. window.y() - 1,
  291. window.width() + 2,
  292. window.height() + 2
  293. };
  294. Color title_color;
  295. Color border_color;
  296. Color border_color2;
  297. Color middle_border_color;
  298. if (&window == m_drag_window.ptr()) {
  299. border_color = m_dragging_window_border_color;
  300. border_color2 = m_dragging_window_border_color2;
  301. title_color = m_dragging_window_title_color;
  302. middle_border_color = Color::White;
  303. } else if (&window == m_active_window.ptr()) {
  304. border_color = m_active_window_border_color;
  305. border_color2 = m_active_window_border_color2;
  306. title_color = m_active_window_title_color;
  307. middle_border_color = Color::MidGray;
  308. } else {
  309. border_color = m_inactive_window_border_color;
  310. border_color2 = m_inactive_window_border_color2;
  311. title_color = m_inactive_window_title_color;
  312. middle_border_color = Color::MidGray;
  313. }
  314. m_back_painter->fill_rect_with_gradient(titlebar_rect, border_color, border_color2);
  315. for (int i = 2; i <= titlebar_inner_rect.height() - 4; i += 2) {
  316. m_back_painter->draw_line({ titlebar_title_rect.right() + 4, titlebar_inner_rect.y() + i }, { close_button_rect.left() - 3, titlebar_inner_rect.y() + i }, border_color);
  317. }
  318. m_back_painter->draw_rect(border_rect, middle_border_color);
  319. m_back_painter->draw_rect(outer_rect, border_color);
  320. m_back_painter->draw_rect(inner_border_rect, border_color);
  321. m_back_painter->draw_text(titlebar_title_rect, window.title(), TextAlignment::CenterLeft, title_color);
  322. if (!s_close_button_bitmap)
  323. s_close_button_bitmap = &CharacterBitmap::create_from_ascii(s_close_button_bitmap_data, s_close_button_bitmap_width, s_close_button_bitmap_height).leak_ref();
  324. m_back_painter->fill_rect_with_gradient(close_button_rect.shrunken(2, 2), Color::LightGray, Color::White);
  325. m_back_painter->draw_rect(close_button_rect, Color::DarkGray, true);
  326. auto x_location = close_button_rect.center();
  327. x_location.move_by(-(s_close_button_bitmap_width / 2), -(s_close_button_bitmap_height / 2));
  328. m_back_painter->draw_bitmap(x_location, *s_close_button_bitmap, Color::Black);
  329. #ifdef DEBUG_WID_IN_TITLE_BAR
  330. Color metadata_color(96, 96, 96);
  331. m_back_painter->draw_text(
  332. titlebar_inner_rect,
  333. String::format("%d:%d", window.pid(), window.window_id()),
  334. TextAlignment::CenterRight,
  335. metadata_color
  336. );
  337. #endif
  338. }
  339. void WSWindowManager::add_window(WSWindow& window)
  340. {
  341. m_windows.set(&window);
  342. m_windows_in_order.append(&window);
  343. if (!active_window())
  344. set_active_window(&window);
  345. }
  346. void WSWindowManager::move_to_front(WSWindow& window)
  347. {
  348. if (m_windows_in_order.tail() != &window)
  349. invalidate(window);
  350. m_windows_in_order.remove(&window);
  351. m_windows_in_order.append(&window);
  352. }
  353. void WSWindowManager::remove_window(WSWindow& window)
  354. {
  355. if (!m_windows.contains(&window))
  356. return;
  357. invalidate(window);
  358. m_windows.remove(&window);
  359. m_windows_in_order.remove(&window);
  360. if (!active_window() && !m_windows.is_empty())
  361. set_active_window(*m_windows.begin());
  362. }
  363. void WSWindowManager::notify_title_changed(WSWindow& window)
  364. {
  365. printf("[WM] WSWindow{%p} title set to '%s'\n", &window, window.title().characters());
  366. invalidate(outer_window_rect(window.rect()));
  367. }
  368. void WSWindowManager::notify_rect_changed(WSWindow& window, const Rect& old_rect, const Rect& new_rect)
  369. {
  370. printf("[WM] WSWindow %p rect changed (%d,%d %dx%d) -> (%d,%d %dx%d)\n", &window, old_rect.x(), old_rect.y(), old_rect.width(), old_rect.height(), new_rect.x(), new_rect.y(), new_rect.width(), new_rect.height());
  371. invalidate(outer_window_rect(old_rect));
  372. invalidate(outer_window_rect(new_rect));
  373. }
  374. void WSWindowManager::handle_menu_mouse_event(WSMenu& menu, WSMouseEvent& event)
  375. {
  376. bool is_hover_with_any_menu_open = event.type() == WSMouseEvent::MouseMove && m_current_menu;
  377. bool is_mousedown_with_left_button = event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left;
  378. bool should_open_menu = &menu != current_menu() && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
  379. if (should_open_menu) {
  380. if (current_menu() == &menu)
  381. return;
  382. close_current_menu();
  383. if (!menu.is_empty()) {
  384. auto& menu_window = menu.ensure_menu_window();
  385. menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() });
  386. menu_window.set_visible(true);
  387. }
  388. m_current_menu = menu.make_weak_ptr();
  389. return;
  390. }
  391. if (event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left) {
  392. close_current_menu();
  393. return;
  394. }
  395. }
  396. void WSWindowManager::close_current_menu()
  397. {
  398. if (m_current_menu && m_current_menu->menu_window())
  399. m_current_menu->menu_window()->set_visible(false);
  400. m_current_menu = nullptr;
  401. }
  402. void WSWindowManager::handle_menubar_mouse_event(WSMouseEvent& event)
  403. {
  404. for_each_active_menubar_menu([&] (WSMenu& menu) {
  405. if (menu.rect_in_menubar().contains(event.position())) {
  406. handle_menu_mouse_event(menu, event);
  407. return false;
  408. }
  409. return true;
  410. });
  411. }
  412. void WSWindowManager::handle_titlebar_mouse_event(WSWindow& window, WSMouseEvent& event)
  413. {
  414. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  415. #ifdef DRAG_DEBUG
  416. printf("[WM] Begin dragging WSWindow{%p}\n", &window);
  417. #endif
  418. m_drag_window = window.make_weak_ptr();;
  419. m_drag_origin = event.position();
  420. m_drag_window_origin = window.position();
  421. invalidate(window);
  422. return;
  423. }
  424. }
  425. void WSWindowManager::handle_close_button_mouse_event(WSWindow& window, WSMouseEvent& event)
  426. {
  427. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  428. WSMessage message(WSMessage::WindowCloseRequest);
  429. window.on_message(message);
  430. return;
  431. }
  432. }
  433. void WSWindowManager::start_window_resize(WSWindow& window, WSMouseEvent& event)
  434. {
  435. constexpr ResizeDirection direction_for_hot_area[3][3] = {
  436. { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
  437. { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
  438. { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
  439. };
  440. Rect window_rect = window.rect();
  441. int window_relative_x = event.x() - window_rect.x();
  442. int window_relative_y = event.y() - window_rect.y();
  443. int hot_area_row = window_relative_y / (window_rect.height() / 3);
  444. int hot_area_column = window_relative_x / (window_rect.width() / 3);
  445. ASSERT(hot_area_row >= 0 && hot_area_row <= 2);
  446. ASSERT(hot_area_column >= 0 && hot_area_column <= 2);
  447. m_resize_direction = direction_for_hot_area[hot_area_row][hot_area_column];
  448. if (m_resize_direction == ResizeDirection::None) {
  449. ASSERT(!m_resize_window);
  450. return;
  451. }
  452. #ifdef RESIZE_DEBUG
  453. printf("[WM] Begin resizing WSWindow{%p}\n", &window);
  454. #endif
  455. m_resize_window = window.make_weak_ptr();;
  456. m_resize_origin = event.position();
  457. m_resize_window_original_rect = window.rect();
  458. m_resize_window->set_has_painted_since_last_resize(true);
  459. invalidate(window);
  460. }
  461. void WSWindowManager::process_mouse_event(WSMouseEvent& event, WSWindow*& event_window)
  462. {
  463. event_window = nullptr;
  464. if (m_drag_window) {
  465. if (event.type() == WSMessage::MouseUp && event.button() == MouseButton::Left) {
  466. #ifdef DRAG_DEBUG
  467. printf("[WM] Finish dragging WSWindow{%p}\n", m_drag_window.ptr());
  468. #endif
  469. invalidate(*m_drag_window);
  470. m_drag_window = nullptr;
  471. return;
  472. }
  473. if (event.type() == WSMessage::MouseMove) {
  474. auto old_window_rect = m_drag_window->rect();
  475. Point pos = m_drag_window_origin;
  476. #ifdef DRAG_DEBUG
  477. dbgprintf("[WM] Dragging [origin: %d,%d] now: %d,%d\n", m_drag_origin.x(), m_drag_origin.y(), event.x(), event.y());
  478. #endif
  479. pos.move_by(event.x() - m_drag_origin.x(), event.y() - m_drag_origin.y());
  480. m_drag_window->set_position_without_repaint(pos);
  481. invalidate(outer_window_rect(old_window_rect));
  482. invalidate(outer_window_rect(m_drag_window->rect()));
  483. return;
  484. }
  485. }
  486. if (m_resize_window) {
  487. if (event.type() == WSMessage::MouseUp && event.button() == MouseButton::Right) {
  488. #ifdef RESIZE_DEBUG
  489. printf("[WM] Finish resizing WSWindow{%p}\n", m_resize_window.ptr());
  490. #endif
  491. WSMessageLoop::the().post_message(*m_resize_window, make<WSResizeEvent>(m_resize_window->rect(), m_resize_window->rect()));
  492. invalidate(*m_resize_window);
  493. m_resize_window = nullptr;
  494. return;
  495. }
  496. if (event.type() == WSMessage::MouseMove) {
  497. auto old_rect = m_resize_window->rect();
  498. int diff_x = event.x() - m_resize_origin.x();
  499. int diff_y = event.y() - m_resize_origin.y();
  500. int change_x = 0;
  501. int change_y = 0;
  502. int change_w = 0;
  503. int change_h = 0;
  504. switch (m_resize_direction) {
  505. case ResizeDirection::DownRight:
  506. change_w = diff_x;
  507. change_h = diff_y;
  508. break;
  509. case ResizeDirection::Right:
  510. change_w = diff_x;
  511. break;
  512. case ResizeDirection::UpRight:
  513. change_w = diff_x;
  514. change_y = diff_y;
  515. change_h = -diff_y;
  516. break;
  517. case ResizeDirection::Up:
  518. change_y = diff_y;
  519. change_h = -diff_y;
  520. break;
  521. case ResizeDirection::UpLeft:
  522. change_x = diff_x;
  523. change_w = -diff_x;
  524. change_y = diff_y;
  525. change_h = -diff_y;
  526. break;
  527. case ResizeDirection::Left:
  528. change_x = diff_x;
  529. change_w = -diff_x;
  530. break;
  531. case ResizeDirection::DownLeft:
  532. change_x = diff_x;
  533. change_w = -diff_x;
  534. change_h = diff_y;
  535. break;
  536. case ResizeDirection::Down:
  537. change_h = diff_y;
  538. break;
  539. default:
  540. ASSERT_NOT_REACHED();
  541. }
  542. auto new_rect = m_resize_window_original_rect;
  543. Size minimum_size { 50, 50 };
  544. new_rect.set_x(new_rect.x() + change_x);
  545. new_rect.set_y(new_rect.y() + change_y);
  546. new_rect.set_width(max(minimum_size.width(), new_rect.width() + change_w));
  547. new_rect.set_height(max(minimum_size.height(), new_rect.height() + change_h));
  548. if (!m_resize_window->size_increment().is_null()) {
  549. int horizontal_incs = (new_rect.width() - m_resize_window->base_size().width()) / m_resize_window->size_increment().width();
  550. new_rect.set_width(m_resize_window->base_size().width() + horizontal_incs * m_resize_window->size_increment().width());
  551. int vertical_incs = (new_rect.height() - m_resize_window->base_size().height()) / m_resize_window->size_increment().height();
  552. new_rect.set_height(m_resize_window->base_size().height() + vertical_incs * m_resize_window->size_increment().height());
  553. }
  554. if (m_resize_window->rect() == new_rect)
  555. return;
  556. #ifdef RESIZE_DEBUG
  557. dbgprintf("[WM] Resizing [original: %s] now: %s\n",
  558. m_resize_window_original_rect.to_string().characters(),
  559. new_rect.to_string().characters());
  560. #endif
  561. m_resize_window->set_rect(new_rect);
  562. if (m_resize_window->has_painted_since_last_resize()) {
  563. m_resize_window->set_has_painted_since_last_resize(false);
  564. dbgprintf("I'm gonna wait for %s\n", new_rect.to_string().characters());
  565. m_resize_window->set_last_lazy_resize_rect(new_rect);
  566. WSMessageLoop::the().post_message(*m_resize_window, make<WSResizeEvent>(old_rect, new_rect));
  567. }
  568. return;
  569. }
  570. }
  571. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  572. if (!window->global_cursor_tracking())
  573. continue;
  574. ASSERT(window->is_visible()); // Maybe this should be supported? Idk. Let's catch it and think about it later.
  575. Point position { event.x() - window->rect().x(), event.y() - window->rect().y() };
  576. auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button());
  577. window->on_message(*local_event);
  578. }
  579. if (menubar_rect().contains(event.position())) {
  580. handle_menubar_mouse_event(event);
  581. return;
  582. }
  583. if (m_current_menu && m_current_menu->menu_window()) {
  584. bool event_is_inside_current_menu = m_current_menu->menu_window()->rect().contains(event.position());
  585. if (!event_is_inside_current_menu) {
  586. if (m_current_menu->hovered_item())
  587. m_current_menu->clear_hovered_item();
  588. if (event.type() == WSMessage::MouseDown || event.type() == WSMessage::MouseUp)
  589. close_current_menu();
  590. }
  591. }
  592. for_each_visible_window_from_front_to_back([&] (WSWindow& window) {
  593. if (window.type() != WSWindowType::Menu && title_bar_rect(window.rect()).contains(event.position())) {
  594. if (event.type() == WSMessage::MouseDown) {
  595. move_to_front(window);
  596. set_active_window(&window);
  597. }
  598. if (close_button_rect_for_window(window.rect()).contains(event.position())) {
  599. handle_close_button_mouse_event(window, event);
  600. return IterationDecision::Abort;
  601. }
  602. handle_titlebar_mouse_event(window, event);
  603. return IterationDecision::Abort;
  604. }
  605. if (window.rect().contains(event.position())) {
  606. if (window.type() != WSWindowType::Menu && event.type() == WSMessage::MouseDown) {
  607. move_to_front(window);
  608. set_active_window(&window);
  609. }
  610. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Right) {
  611. start_window_resize(window, event);
  612. return IterationDecision::Abort;
  613. }
  614. event_window = &window;
  615. // FIXME: Should we just alter the coordinates of the existing MouseEvent and pass it through?
  616. Point position { event.x() - window.rect().x(), event.y() - window.rect().y() };
  617. auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button());
  618. window.on_message(*local_event);
  619. return IterationDecision::Abort;
  620. }
  621. return IterationDecision::Continue;
  622. });
  623. }
  624. template<typename Callback>
  625. IterationDecision WSWindowManager::for_each_visible_window_of_type_from_back_to_front(WSWindowType type, Callback callback)
  626. {
  627. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  628. if (!window->is_visible())
  629. continue;
  630. if (window->type() != type)
  631. continue;
  632. if (callback(*window) == IterationDecision::Abort)
  633. return IterationDecision::Abort;
  634. }
  635. return IterationDecision::Continue;
  636. }
  637. template<typename Callback>
  638. IterationDecision WSWindowManager::for_each_visible_window_from_back_to_front(Callback callback)
  639. {
  640. if (for_each_visible_window_of_type_from_back_to_front(WSWindowType::Normal, callback) == IterationDecision::Abort)
  641. return IterationDecision::Abort;
  642. return for_each_visible_window_of_type_from_back_to_front(WSWindowType::Menu, callback);
  643. }
  644. template<typename Callback>
  645. IterationDecision WSWindowManager::for_each_visible_window_of_type_from_front_to_back(WSWindowType type, Callback callback)
  646. {
  647. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  648. if (!window->is_visible())
  649. continue;
  650. if (window->type() != type)
  651. continue;
  652. if (callback(*window) == IterationDecision::Abort)
  653. return IterationDecision::Abort;
  654. }
  655. return IterationDecision::Continue;
  656. }
  657. template<typename Callback>
  658. IterationDecision WSWindowManager::for_each_visible_window_from_front_to_back(Callback callback)
  659. {
  660. if (for_each_visible_window_of_type_from_front_to_back(WSWindowType::Menu, callback) == IterationDecision::Abort)
  661. return IterationDecision::Abort;
  662. return for_each_visible_window_of_type_from_front_to_back(WSWindowType::Normal, callback);
  663. }
  664. void WSWindowManager::compose()
  665. {
  666. auto dirty_rects = move(m_dirty_rects);
  667. auto cursor_location = m_screen.cursor_location();
  668. dirty_rects.add(m_last_cursor_rect);
  669. dirty_rects.add({ cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() });
  670. #ifdef DEBUG_COUNTERS
  671. dbgprintf("[WM] compose #%u (%u rects)\n", ++m_compose_count, dirty_rects.rects().size());
  672. #endif
  673. auto any_opaque_window_contains_rect = [this] (const Rect& r) {
  674. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  675. if (!window->is_visible())
  676. continue;
  677. if (window->opacity() < 1.0f)
  678. continue;
  679. if (window->has_alpha_channel()) {
  680. // FIXME: Just because the window has an alpha channel doesn't mean it's not opaque.
  681. // Maybe there's some way we could know this?
  682. continue;
  683. }
  684. if (outer_window_rect(window->rect()).contains(r))
  685. return true;
  686. }
  687. return false;
  688. };
  689. auto any_dirty_rect_intersects_window = [&dirty_rects] (const WSWindow& window) {
  690. auto window_rect = outer_window_rect(window.rect());
  691. for (auto& dirty_rect : dirty_rects.rects()) {
  692. if (dirty_rect.intersects(window_rect))
  693. return true;
  694. }
  695. return false;
  696. };
  697. for (auto& dirty_rect : dirty_rects.rects()) {
  698. if (any_opaque_window_contains_rect(dirty_rect))
  699. continue;
  700. if (!m_wallpaper)
  701. m_back_painter->fill_rect(dirty_rect, m_background_color);
  702. else
  703. m_back_painter->blit(dirty_rect.location(), *m_wallpaper, dirty_rect);
  704. }
  705. for_each_visible_window_from_back_to_front([&] (WSWindow& window) {
  706. RetainPtr<GraphicsBitmap> backing_store = window.backing_store();
  707. if (!backing_store)
  708. return IterationDecision::Continue;
  709. if (!any_dirty_rect_intersects_window(window))
  710. return IterationDecision::Continue;
  711. for (auto& dirty_rect : dirty_rects.rects()) {
  712. m_back_painter->set_clip_rect(dirty_rect);
  713. paint_window_frame(window);
  714. Rect dirty_rect_in_window_coordinates = Rect::intersection(dirty_rect, window.rect());
  715. if (dirty_rect_in_window_coordinates.is_empty())
  716. continue;
  717. dirty_rect_in_window_coordinates.set_x(dirty_rect_in_window_coordinates.x() - window.x());
  718. dirty_rect_in_window_coordinates.set_y(dirty_rect_in_window_coordinates.y() - window.y());
  719. auto dst = window.position();
  720. dst.move_by(dirty_rect_in_window_coordinates.location());
  721. if (window.opacity() == 1.0f)
  722. m_back_painter->blit(dst, *backing_store, dirty_rect_in_window_coordinates);
  723. else
  724. m_back_painter->blit_with_opacity(dst, *backing_store, dirty_rect_in_window_coordinates, window.opacity());
  725. m_back_painter->clear_clip_rect();
  726. }
  727. m_back_painter->clear_clip_rect();
  728. return IterationDecision::Continue;
  729. });
  730. draw_menubar();
  731. draw_cursor();
  732. if (m_flash_flush) {
  733. for (auto& rect : dirty_rects.rects())
  734. m_front_painter->fill_rect(rect, Color::Yellow);
  735. }
  736. flip_buffers();
  737. for (auto& r : dirty_rects.rects())
  738. flush(r);
  739. }
  740. void WSWindowManager::invalidate_cursor()
  741. {
  742. auto cursor_location = m_screen.cursor_location();
  743. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  744. invalidate(cursor_rect);
  745. }
  746. Rect WSWindowManager::menubar_rect() const
  747. {
  748. return { 0, 0, m_screen_rect.width(), 16 };
  749. }
  750. void WSWindowManager::draw_menubar()
  751. {
  752. m_back_painter->fill_rect(menubar_rect(), Color::LightGray);
  753. m_back_painter->draw_line({ 0, menubar_rect().bottom() }, { menubar_rect().right(), menubar_rect().bottom() }, Color::White);
  754. for_each_active_menubar_menu([&] (WSMenu& menu) {
  755. Color text_color = Color::Black;
  756. if (&menu == current_menu()) {
  757. m_back_painter->fill_rect(menu.rect_in_menubar(), menu_selection_color());
  758. text_color = Color::White;
  759. }
  760. m_back_painter->draw_text(menu.text_rect_in_menubar(), menu.name(), TextAlignment::CenterLeft, text_color);
  761. return true;
  762. });
  763. time_t now = time(nullptr);
  764. auto* tm = localtime(&now);
  765. auto time_text = String::format("%4u-%02u-%02u %02u:%02u:%02u\n",
  766. tm->tm_year + 1900,
  767. tm->tm_mon + 1,
  768. tm->tm_mday,
  769. tm->tm_hour,
  770. tm->tm_min,
  771. tm->tm_sec);
  772. auto time_rect = menubar_rect().translated(-(menubar_menu_margin() / 2), 0);
  773. m_back_painter->draw_text(time_rect, time_text, TextAlignment::CenterRight, Color::Black);
  774. }
  775. void WSWindowManager::draw_cursor()
  776. {
  777. auto cursor_location = m_screen.cursor_location();
  778. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  779. Color inner_color = Color::White;
  780. Color outer_color = Color::Black;
  781. if (m_screen.left_mouse_button_pressed())
  782. swap(inner_color, outer_color);
  783. m_back_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_inner, inner_color);
  784. m_back_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_outer, outer_color);
  785. m_last_cursor_rect = cursor_rect;
  786. }
  787. void WSWindowManager::on_message(WSMessage& message)
  788. {
  789. if (message.is_mouse_event()) {
  790. WSWindow* event_window = nullptr;
  791. process_mouse_event(static_cast<WSMouseEvent&>(message), event_window);
  792. set_hovered_window(event_window);
  793. return;
  794. }
  795. if (message.is_key_event()) {
  796. // FIXME: This is a good place to hook key events globally. :)
  797. if (m_active_window)
  798. return m_active_window->on_message(message);
  799. return;
  800. }
  801. if (message.type() == WSMessage::WM_DeferredCompose) {
  802. m_pending_compose_event = false;
  803. compose();
  804. return;
  805. }
  806. }
  807. void WSWindowManager::set_active_window(WSWindow* window)
  808. {
  809. if (window->type() == WSWindowType::Menu) {
  810. dbgprintf("WSWindowManager: Attempted to make a menu window active.\n");
  811. return;
  812. }
  813. if (window == m_active_window.ptr())
  814. return;
  815. if (auto* previously_active_window = m_active_window.ptr()) {
  816. WSMessageLoop::the().post_message(*previously_active_window, make<WSMessage>(WSMessage::WindowDeactivated));
  817. invalidate(*previously_active_window);
  818. }
  819. m_active_window = window->make_weak_ptr();
  820. if (m_active_window) {
  821. WSMessageLoop::the().post_message(*m_active_window, make<WSMessage>(WSMessage::WindowActivated));
  822. invalidate(*m_active_window);
  823. auto* client = window->client();
  824. ASSERT(client);
  825. set_current_menubar(client->app_menubar());
  826. }
  827. }
  828. void WSWindowManager::set_hovered_window(WSWindow* window)
  829. {
  830. if (m_hovered_window.ptr() == window)
  831. return;
  832. if (m_hovered_window)
  833. WSMessageLoop::the().post_message(*m_hovered_window, make<WSMessage>(WSMessage::WindowLeft));
  834. m_hovered_window = window ? window->make_weak_ptr() : nullptr;
  835. if (m_hovered_window)
  836. WSMessageLoop::the().post_message(*m_hovered_window, make<WSMessage>(WSMessage::WindowEntered));
  837. }
  838. void WSWindowManager::invalidate()
  839. {
  840. m_dirty_rects.clear_with_capacity();
  841. invalidate(m_screen_rect);
  842. }
  843. void WSWindowManager::recompose_immediately()
  844. {
  845. m_dirty_rects.clear_with_capacity();
  846. invalidate(m_screen_rect, false);
  847. }
  848. void WSWindowManager::invalidate(const Rect& a_rect, bool should_schedule_compose_event)
  849. {
  850. auto rect = Rect::intersection(a_rect, m_screen_rect);
  851. if (rect.is_empty())
  852. return;
  853. m_dirty_rects.add(rect);
  854. if (should_schedule_compose_event && !m_pending_compose_event) {
  855. WSMessageLoop::the().post_message(*this, make<WSMessage>(WSMessage::WM_DeferredCompose));
  856. m_pending_compose_event = true;
  857. }
  858. }
  859. void WSWindowManager::invalidate(const WSWindow& window)
  860. {
  861. if (window.type() == WSWindowType::Menu) {
  862. invalidate(menu_window_rect(window.rect()));
  863. return;
  864. }
  865. if (window.type() == WSWindowType::Normal) {
  866. invalidate(outer_window_rect(window.rect()));
  867. return;
  868. }
  869. ASSERT_NOT_REACHED();
  870. }
  871. void WSWindowManager::invalidate(const WSWindow& window, const Rect& rect)
  872. {
  873. if (rect.is_empty()) {
  874. invalidate(window);
  875. return;
  876. }
  877. auto outer_rect = outer_window_rect(window.rect());
  878. auto inner_rect = rect;
  879. inner_rect.move_by(window.position());
  880. // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect.
  881. inner_rect.intersect(outer_rect);
  882. invalidate(inner_rect);
  883. }
  884. void WSWindowManager::flush(const Rect& a_rect)
  885. {
  886. auto rect = Rect::intersection(a_rect, m_screen_rect);
  887. #ifdef DEBUG_COUNTERS
  888. dbgprintf("[WM] flush #%u (%d,%d %dx%d)\n", ++m_flush_count, rect.x(), rect.y(), rect.width(), rect.height());
  889. #endif
  890. RGBA32* front_ptr = m_front_bitmap->scanline(rect.y()) + rect.x();
  891. RGBA32* back_ptr = m_back_bitmap->scanline(rect.y()) + rect.x();
  892. size_t pitch = m_back_bitmap->pitch();
  893. for (int y = 0; y < rect.height(); ++y) {
  894. fast_dword_copy(back_ptr, front_ptr, rect.width());
  895. front_ptr = (RGBA32*)((byte*)front_ptr + pitch);
  896. back_ptr = (RGBA32*)((byte*)back_ptr + pitch);
  897. }
  898. }
  899. void WSWindowManager::close_menu(WSMenu& menu)
  900. {
  901. if (current_menu() == &menu)
  902. close_current_menu();
  903. }
  904. void WSWindowManager::close_menubar(WSMenuBar& menubar)
  905. {
  906. if (current_menubar() == &menubar)
  907. set_current_menubar(nullptr);
  908. }
  909. const WSClientConnection* WSWindowManager::active_client() const
  910. {
  911. if (m_active_window)
  912. return m_active_window->client();
  913. return 0;
  914. }
  915. void WSWindowManager::notify_client_changed_app_menubar(WSClientConnection& client)
  916. {
  917. if (active_client() == &client)
  918. set_current_menubar(client.app_menubar());
  919. invalidate();
  920. }