Luna/gui/apps/editor/main.cpp

54 lines
1.5 KiB
C++
Raw Normal View History

2024-02-11 16:10:17 +00:00
/**
* @file main.cpp
* @author apio (cloudapio.eu)
* @brief Graphical text editor.
*
* @copyright Copyright (c) 2024, the Luna authors.
*
*/
#include "EditorWidget.h"
2024-02-27 19:11:14 +00:00
#include <os/ArgumentParser.h>
#include <os/File.h>
2024-02-11 16:10:17 +00:00
#include <ui/App.h>
#include <ui/Dialog.h>
2024-02-11 16:10:17 +00:00
2024-02-27 19:11:14 +00:00
Result<int> luna_main(int argc, char** argv)
2024-02-11 16:10:17 +00:00
{
2024-02-27 19:11:14 +00:00
StringView path;
os::ArgumentParser parser;
parser.add_description("A graphical text editor"_sv);
parser.add_system_program_info("editor"_sv);
parser.add_positional_argument(path, "path", false);
parser.parse(argc, argv);
2024-02-11 16:10:17 +00:00
ui::App app;
TRY(app.init());
auto* window = TRY(ui::Window::create(ui::Rect { 200, 300, 600, 600 }));
window->set_background(ui::Color::from_rgb(40, 40, 40));
window->set_title("Text Editor");
app.set_main_window(window);
auto* editor = TRY(make<EditorWidget>(ui::Font::default_font()));
window->set_main_widget(*editor);
if (!path.is_empty()) editor->load_file(path);
2024-02-11 16:10:17 +00:00
TRY(window->add_keyboard_shortcut({ moon::K_CH26, ui::Mod_Ctrl }, true, [&](ui::Shortcut) {
auto result = editor->save_file();
if (result.has_error())
{
os::eprintln("Failed to save file %s: %s", editor->path().name().chars(), result.error_string());
ui::Dialog::show_message("Error", "Failed to save file");
}
}));
TRY(window->add_keyboard_shortcut({ moon::K_CH26, ui::Mod_Ctrl | ui::Mod_Shift }, true,
[&](ui::Shortcut) { editor->save_file_as(); }));
2024-02-11 16:10:17 +00:00
window->draw();
return app.run();
}