forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.cpp
201 lines (161 loc) · 4.15 KB
/
console.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Aseprite
// Copyright (C) 2018-2021 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/console.h"
#include "app/app.h"
#include "app/context.h"
#include "app/modules/gui.h"
#include "app/ui/status_bar.h"
#include "base/bind.h"
#include "base/memory.h"
#include "base/string.h"
#include "ui/system.h"
#include "ui/ui.h"
#include <cstdarg>
#include <cstdio>
#include <memory>
namespace app {
using namespace ui;
class Console::ConsoleWindow : public Window {
public:
ConsoleWindow() : Window(Window::WithTitleBar, "Console"),
m_textbox("", WORDWRAP),
m_button("Cancel") {
m_button.Click.connect([this]{ closeWindow(&m_button); });
// When the window is closed, we clear the text
Close.connect(
[this]{
m_textbox.setText(std::string());
});
m_view.attachToView(&m_textbox);
Grid* grid = new Grid(1, false);
grid->addChildInCell(&m_view, 1, 1, HORIZONTAL | VERTICAL);
grid->addChildInCell(&m_button, 1, 1, CENTER);
addChild(grid);
m_textbox.setFocusMagnet(true);
m_button.setFocusMagnet(true);
m_view.setExpansive(true);
initTheme();
}
void addMessage(const std::string& msg) {
if (!m_hasText) {
m_hasText = true;
centerConsole();
}
m_textbox.setText(m_textbox.text() + msg);
}
bool isConsoleVisible() const {
return (m_hasText && isVisible());
}
void centerConsole() {
initTheme();
remapWindow();
setBounds(gfx::Rect(0, 0, ui::display_w()*9/10, ui::display_h()*6/10));
centerWindow();
invalidate();
}
private:
bool onProcessMessage(ui::Message* msg) override {
switch (msg->type()) {
case ui::kKeyDownMessage:
#if defined __APPLE__
if (msg->onlyCmdPressed())
#else
if (msg->onlyCtrlPressed())
#endif
{
if (static_cast<KeyMessage*>(msg)->scancode() == kKeyC)
set_clipboard_text(m_textbox.text());
}
break;
}
return Window::onProcessMessage(msg);
}
void onInitTheme(InitThemeEvent& ev) override {
Window::onInitTheme(ev);
m_button.setMinSize(gfx::Size(60*ui::guiscale(), 0));
}
View m_view;
TextBox m_textbox;
Button m_button;
bool m_hasText = false;
};
int Console::m_consoleCounter = 0;
std::unique_ptr<Console::ConsoleWindow> Console::m_console = nullptr;
Console::Console(Context* ctx)
: m_withUI(false)
{
if (!ui::is_ui_thread())
return;
if (ctx)
m_withUI = (ctx->isUIAvailable());
else
m_withUI =
(App::instance() &&
App::instance()->isGui() &&
Manager::getDefault() &&
Manager::getDefault()->getDisplay());
if (!m_withUI)
return;
++m_consoleCounter;
if (m_console || m_consoleCounter > 1)
return;
m_console.reset(new ConsoleWindow);
}
Console::~Console()
{
if (!m_withUI)
return;
--m_consoleCounter;
if (m_console && m_console->isConsoleVisible()) {
m_console->manager()->attractFocus(m_console.get());
m_console->openWindowInForeground();
}
if (m_consoleCounter == 0)
m_console.reset();
}
void Console::printf(const char* format, ...)
{
std::va_list ap;
va_start(ap, format);
std::string msg = base::string_vprintf(format, ap);
va_end(ap);
if (!m_withUI || !m_console) {
fputs(msg.c_str(), stdout);
fflush(stdout);
return;
}
// Open the window
if (!m_console->isVisible()) {
m_console->openWindow();
ui::Manager::getDefault()->invalidate();
}
// Update the textbox
m_console->addMessage(msg);
}
// static
void Console::showException(const std::exception& e)
{
if (!ui::is_ui_thread()) {
LOG(ERROR, "A problem has occurred.\n\nDetails:\n%s\n", e.what());
return;
}
Console console;
if (typeid(e) == typeid(std::bad_alloc))
console.printf("There is not enough memory to complete the action.");
else
console.printf("A problem has occurred.\n\nDetails:\n%s\n", e.what());
}
// static
void Console::notifyNewDisplayConfiguration()
{
if (m_console)
m_console->centerConsole();
}
} // namespace app