1// Aseprite
2// Copyright (C) 2020-2021 Igara Studio S.A.
3// Copyright (C) 2001-2017 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "app/app.h"
13#include "app/app_menus.h"
14#include "app/commands/command.h"
15#include "app/ui/main_menu_bar.h"
16#include "app/ui/main_window.h"
17#include "app/ui/status_bar.h"
18#include "fmt/format.h"
19#include "ui/scale.h"
20#include "ui/system.h"
21#include "ui/theme.h"
22
23#if defined _WIN32 && defined ENABLE_DEVMODE
24 #include <windows.h>
25
26 #include <psapi.h>
27#endif
28
29namespace app {
30
31class RefreshCommand : public Command {
32public:
33 RefreshCommand();
34
35protected:
36 void onExecute(Context* context) override;
37
38private:
39 void postCancelMenuLoop();
40};
41
42RefreshCommand::RefreshCommand()
43 : Command(CommandId::Refresh(), CmdUIOnlyFlag)
44{
45}
46
47void RefreshCommand::onExecute(Context* context)
48{
49 if (!context->isUIAvailable())
50 return;
51
52 // Close the current menu loop just in case if there is a menu popup
53 // open, and then enqueue the postReload() function after all menus
54 // are closed.
55 App::instance()->mainWindow()->getMenuBar()->cancelMenuLoop();
56
57 // Now that all menus are going to be closed (the final close
58 // messages are enqueued in the UI message queue), we can queue a
59 // function call that will reload all menus.
60 ui::execute_from_ui_thread(
61 [this]{
62 postCancelMenuLoop();
63 });
64}
65
66void RefreshCommand::postCancelMenuLoop()
67{
68 // Reload menus (mainly to reload the File > Scripts menu)
69 App::instance()->mainWindow()->getMenuBar()->reload();
70
71 // Reload theme
72 ui::set_theme(ui::get_theme(),
73 ui::guiscale());
74
75 // Redraw screen
76 app_refresh_screen();
77
78 // Print memory information
79#if defined _WIN32 && defined ENABLE_DEVMODE
80 {
81 PROCESS_MEMORY_COUNTERS pmc;
82 if (::GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) {
83 StatusBar::instance()->showTip(
84 1000,
85 fmt::format("Current memory: {:.2f} MB ({})\n"
86 "Peak of memory: {:.2f} MB ({})",
87 pmc.WorkingSetSize / 1024.0 / 1024.0, pmc.WorkingSetSize,
88 pmc.PeakWorkingSetSize / 1024.0 / 1024.0, pmc.PeakWorkingSetSize));
89 }
90 }
91#endif
92}
93
94Command* CommandFactory::createRefreshCommand()
95{
96 return new RefreshCommand;
97}
98
99} // namespace app
100