mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-29 02:50:26 +00:00
Calculator: Add support for copy+paste using system clipboard
This commit is contained in:
parent
1ba1ca76d8
commit
9bc3c3c962
Notes:
sideshowbarker
2024-07-18 21:56:56 +09:00
Author: https://github.com/bcoles Commit: https://github.com/SerenityOS/serenity/commit/9bc3c3c9628 Pull-request: https://github.com/SerenityOS/serenity/pull/5508 Reviewed-by: https://github.com/awesomekling
3 changed files with 29 additions and 1 deletions
|
@ -147,6 +147,17 @@ void CalculatorWidget::add_digit_button(GUI::Button& button, int digit)
|
|||
};
|
||||
}
|
||||
|
||||
String CalculatorWidget::get_entry()
|
||||
{
|
||||
return m_entry->text();
|
||||
}
|
||||
|
||||
void CalculatorWidget::set_entry(double value)
|
||||
{
|
||||
m_keypad.set_value(value);
|
||||
update_display();
|
||||
}
|
||||
|
||||
void CalculatorWidget::update_display()
|
||||
{
|
||||
m_entry->set_text(m_keypad.to_string());
|
||||
|
|
|
@ -36,6 +36,8 @@ class CalculatorWidget final : public GUI::Widget {
|
|||
C_OBJECT(CalculatorWidget)
|
||||
public:
|
||||
virtual ~CalculatorWidget() override;
|
||||
String get_entry();
|
||||
void set_entry(double);
|
||||
|
||||
private:
|
||||
CalculatorWidget();
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#include "CalculatorWidget.h"
|
||||
#include <LibGUI/Action.h>
|
||||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/Clipboard.h>
|
||||
#include <LibGUI/Icon.h>
|
||||
#include <LibGUI/Menu.h>
|
||||
#include <LibGUI/MenuBar.h>
|
||||
|
@ -62,7 +63,7 @@ int main(int argc, char** argv)
|
|||
window->set_resizable(false);
|
||||
window->resize(254, 213);
|
||||
|
||||
window->set_main_widget<CalculatorWidget>();
|
||||
auto& widget = window->set_main_widget<CalculatorWidget>();
|
||||
|
||||
window->show();
|
||||
window->set_icon(app_icon.bitmap_for_size(16));
|
||||
|
@ -75,6 +76,20 @@ int main(int argc, char** argv)
|
|||
return;
|
||||
}));
|
||||
|
||||
auto& edit_menu = menubar->add_menu("Edit");
|
||||
edit_menu.add_action(GUI::Action::create("Copy", { Mod_Ctrl, Key_C }, Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-copy.png"), [&](const GUI::Action&) {
|
||||
GUI::Clipboard::the().set_plain_text(widget.get_entry());
|
||||
}));
|
||||
edit_menu.add_action(GUI::Action::create("Paste", { Mod_Ctrl, Key_V }, Gfx::Bitmap::load_from_file("/res/icons/16x16/paste.png"), [&](const GUI::Action&) {
|
||||
auto clipboard = GUI::Clipboard::the().data_and_type();
|
||||
if (clipboard.mime_type == "text/plain") {
|
||||
if (!clipboard.data.is_empty()) {
|
||||
auto data = atof(StringView(clipboard.data).to_string().characters());
|
||||
widget.set_entry(data);
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
auto& help_menu = menubar->add_menu("Help");
|
||||
help_menu.add_action(GUI::CommonActions::make_about_action("Calculator", app_icon, window));
|
||||
|
||||
|
|
Loading…
Reference in a new issue