main.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "KeyboardMapperWidget.h"
  27. #include <LibCore/ArgsParser.h>
  28. #include <LibGUI/AboutDialog.h>
  29. #include <LibGUI/Action.h>
  30. #include <LibGUI/Application.h>
  31. #include <LibGUI/FilePicker.h>
  32. #include <LibGUI/Icon.h>
  33. #include <LibGUI/Menu.h>
  34. #include <LibGUI/MenuBar.h>
  35. int main(int argc, char** argv)
  36. {
  37. const char* path = nullptr;
  38. Core::ArgsParser args_parser;
  39. args_parser.add_positional_argument(path, "Keyboard character mapping file.", "file", Core::ArgsParser::Required::No);
  40. args_parser.parse(argc, argv);
  41. GUI::Application app(argc, argv);
  42. auto app_icon = GUI::Icon::default_icon("app-keyboard-mapper");
  43. auto window = GUI::Window::construct();
  44. window->set_title("KeyboardMapper");
  45. window->set_icon(app_icon.bitmap_for_size(16));
  46. window->set_main_widget<KeyboardMapperWidget>();
  47. window->resize(775, 315);
  48. window->move_to(50, 50);
  49. window->set_resizable(false);
  50. window->show();
  51. auto keyboard_mapper_widget = (KeyboardMapperWidget*)window->main_widget();
  52. if (path != nullptr) {
  53. keyboard_mapper_widget->load_from_file(path);
  54. } else {
  55. keyboard_mapper_widget->load_from_file("/res/keymaps/en.json");
  56. }
  57. // Actions
  58. auto open_action = GUI::CommonActions::make_open_action(
  59. [&](auto&) {
  60. Optional<String> path = GUI::FilePicker::get_open_filepath("Open");
  61. if (path.has_value()) {
  62. keyboard_mapper_widget->load_from_file(path.value());
  63. }
  64. });
  65. auto save_action = GUI::CommonActions::make_save_action(
  66. [&](auto&) {
  67. keyboard_mapper_widget->save();
  68. });
  69. auto save_as_action = GUI::Action::create("Save as...", { Mod_Ctrl | Mod_Shift, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"),
  70. [&](auto&) {
  71. String m_name = "Unnamed";
  72. Optional<String> save_path = GUI::FilePicker::get_save_filepath(m_name, "json");
  73. if (!save_path.has_value())
  74. return;
  75. keyboard_mapper_widget->save_to_file(save_path.value());
  76. });
  77. auto quit_action = GUI::CommonActions::make_quit_action(
  78. [&](auto&) {
  79. app.quit();
  80. });
  81. auto about_action = GUI::Action::create("About",
  82. [&](auto&) {
  83. GUI::AboutDialog::show("KeyboardMapper", app_icon.bitmap_for_size(32), window);
  84. });
  85. // Menu
  86. auto menubar = GUI::MenuBar::construct();
  87. auto& app_menu = menubar->add_menu("KeyboardMapper");
  88. app_menu.add_action(open_action);
  89. app_menu.add_action(save_action);
  90. app_menu.add_action(save_as_action);
  91. app_menu.add_separator();
  92. app_menu.add_action(quit_action);
  93. auto& help_menu = menubar->add_menu("Help");
  94. help_menu.add_action(about_action);
  95. app.set_menubar(move(menubar));
  96. return app.exec();
  97. }