浏览代码

LibGUI: Add fallible try_create_checkable() Action

thankyouverycool 2 年之前
父节点
当前提交
7252a1aa4d
共有 2 个文件被更改,包括 7 次插入0 次删除
  1. 5 0
      Userland/Libraries/LibGUI/Action.cpp
  2. 2 0
      Userland/Libraries/LibGUI/Action.h

+ 5 - 0
Userland/Libraries/LibGUI/Action.cpp

@@ -63,6 +63,11 @@ NonnullRefPtr<Action> Action::create_checkable(String text, Shortcut const& shor
     return adopt_ref(*new Action(move(text), shortcut, Shortcut {}, move(icon), move(callback), parent, true));
 }
 
+ErrorOr<NonnullRefPtr<Action>> Action::try_create_checkable(String text, Shortcut const& shortcut, Function<void(Action&)> callback, Core::Object* parent)
+{
+    return adopt_nonnull_ref_or_enomem(new (nothrow) Action(move(text), shortcut, Shortcut {}, move(callback), parent, true));
+}
+
 RefPtr<Action> Action::find_action_for_shortcut(Core::Object& object, Shortcut const& shortcut)
 {
     RefPtr<Action> found_action = nullptr;

+ 2 - 0
Userland/Libraries/LibGUI/Action.h

@@ -76,6 +76,8 @@ public:
     static NonnullRefPtr<Action> create_checkable(String text, Shortcut const& shortcut, Function<void(Action&)> callback, Core::Object* parent = nullptr);
     static NonnullRefPtr<Action> create_checkable(String text, Shortcut const& shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent = nullptr);
 
+    static ErrorOr<NonnullRefPtr<Action>> try_create_checkable(String text, Shortcut const& shortcut, Function<void(Action&)> callback, Core::Object* parent = nullptr);
+
     static RefPtr<Action> find_action_for_shortcut(Core::Object& object, Shortcut const& shortcut);
 
     virtual ~Action() override;