Skip to content

Commit

Permalink
cleaned up clipboard support
Browse files Browse the repository at this point in the history
  • Loading branch information
efroemling committed Oct 26, 2023
1 parent f4aec57 commit f0ebe2b
Show file tree
Hide file tree
Showing 20 changed files with 207 additions and 199 deletions.
56 changes: 28 additions & 28 deletions .efrocachemap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### 1.7.28 (build 21512, api 8, 2023-10-26)
### 1.7.28 (build 21516, api 8, 2023-10-26)

- Massively cleaned up code related to rendering and window systems (OpenGL,
SDL, etc). This code had been growing into a nasty tangle for 15 years
Expand Down
2 changes: 1 addition & 1 deletion src/assets/ba_data/python/baenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

# Build number and version of the ballistica binary we expect to be
# using.
TARGET_BALLISTICA_BUILD = 21512
TARGET_BALLISTICA_BUILD = 21516
TARGET_BALLISTICA_VERSION = '1.7.28'


Expand Down
4 changes: 2 additions & 2 deletions src/assets/ba_data/python/bauiv1lib/gather/privatetab.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,9 @@ def _build_join_tab(self) -> None:
scale=1.5,
size=(300, 50),
editable=True,
max_chars=20,
description=bui.Lstr(resource='gatherWindow.partyCodeText'),
autoselect=True,
maxwidth=250,
h_align='left',
v_align='center',
text='',
Expand Down Expand Up @@ -962,7 +962,7 @@ def _join_connect_press(self) -> None:
code = cast(str, bui.textwidget(query=self._join_party_code_text))
if not code:
bui.screenmessage(
bui.Lstr(resource='internal.invalidAddressErrorText'),
bui.Lstr(translate=('serverResponses', 'Invalid code.')),
color=(1, 0, 0),
)
bui.getsound('error').play()
Expand Down
2 changes: 1 addition & 1 deletion src/assets/ba_data/python/bauiv1lib/mainmenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ def _resume(self) -> None:
bui.app.classic.resume()
if self._root_widget:
bui.containerwidget(edit=self._root_widget, transition='out_right')
bui.app.ui_v1.clear_main_menu_window()
bui.app.ui_v1.clear_main_menu_window(transition='out_right')

# If there's callbacks waiting for this window to go away, call them.
for call in bui.app.ui_v1.main_menu_resume_callbacks:
Expand Down
54 changes: 54 additions & 0 deletions src/ballistica/base/app_adapter/app_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,58 @@ auto AppAdapter::GetGraphicsClientContext() -> GraphicsClientContext* {
auto AppAdapter::GetKeyRepeatDelay() -> float { return 0.3f; }
auto AppAdapter::GetKeyRepeatInterval() -> float { return 0.08f; }

auto AppAdapter::ClipboardIsSupported() -> bool {
// We only call our actual virtual function once.
if (!have_clipboard_is_supported_) {
clipboard_is_supported_ = DoClipboardIsSupported();
have_clipboard_is_supported_ = true;
}
return clipboard_is_supported_;
}

auto AppAdapter::ClipboardHasText() -> bool {
// If subplatform says they don't support clipboards, don't even ask.
if (!ClipboardIsSupported()) {
return false;
}
return DoClipboardHasText();
}

void AppAdapter::ClipboardSetText(const std::string& text) {
// If subplatform says they don't support clipboards, this is an error.
if (!ClipboardIsSupported()) {
throw Exception("ClipboardSetText called with no clipboard support.",
PyExcType::kRuntime);
}
DoClipboardSetText(text);
}

auto AppAdapter::ClipboardGetText() -> std::string {
// If subplatform says they don't support clipboards, this is an error.
if (!ClipboardIsSupported()) {
throw Exception("ClipboardGetText called with no clipboard support.",
PyExcType::kRuntime);
}
return DoClipboardGetText();
}

auto AppAdapter::DoClipboardIsSupported() -> bool { return false; }

auto AppAdapter::DoClipboardHasText() -> bool {
// Shouldn't get here since we default to no clipboard support.
FatalError("Shouldn't get here.");
return false;
}

void AppAdapter::DoClipboardSetText(const std::string& text) {
// Shouldn't get here since we default to no clipboard support.
FatalError("Shouldn't get here.");
}

auto AppAdapter::DoClipboardGetText() -> std::string {
// Shouldn't get here since we default to no clipboard support.
FatalError("Shouldn't get here.");
return "";
}

} // namespace ballistica::base
26 changes: 25 additions & 1 deletion src/ballistica/base/app_adapter/app_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,22 @@ class AppAdapter {
virtual auto GetKeyRepeatDelay() -> float;
virtual auto GetKeyRepeatInterval() -> float;

/// Return whether clipboard operations are supported at all. This gets
/// called when determining whether to display clipboard related UI
/// elements/etc.
auto ClipboardIsSupported() -> bool;

/// Return whether there is currently text on the clipboard.
auto ClipboardHasText() -> bool;

/// Set current clipboard text. Raises an Exception if clipboard is
/// unsupported.
void ClipboardSetText(const std::string& text);

/// Return current text from the clipboard. Raises an Exception if
/// clipboard is unsupported or if there's no text on the clipboard.
auto ClipboardGetText() -> std::string;

protected:
AppAdapter();
virtual ~AppAdapter();
Expand All @@ -219,10 +235,18 @@ class AppAdapter {
/// context. By default this is simply the main thread.
virtual void DoPushGraphicsContextRunnable(Runnable* runnable);

virtual auto DoClipboardIsSupported() -> bool;
virtual auto DoClipboardHasText() -> bool;
virtual void DoClipboardSetText(const std::string& text);
virtual auto DoClipboardGetText() -> std::string;

private:
void OnAppSuspend_();
void OnAppUnsuspend_();
bool app_suspended_{};

bool app_suspended_ : 1 {};
bool have_clipboard_is_supported_ : 1 {};
bool clipboard_is_supported_ : 1 {};
};

} // namespace ballistica::base
Expand Down
37 changes: 37 additions & 0 deletions src/ballistica/base/app_adapter/app_adapter_apple.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "ballistica/base/graphics/graphics.h"
#include "ballistica/base/graphics/graphics_server.h"
#include "ballistica/base/logic/logic.h"
#include "ballistica/base/platform/apple/apple_utils.h"
#include "ballistica/base/platform/apple/from_swift.h"
#include "ballistica/base/support/app_config.h"
#include "ballistica/shared/ballistica.h"
Expand Down Expand Up @@ -245,6 +246,42 @@ auto AppAdapterApple::GetKeyRepeatInterval() -> float {
#endif
}

auto AppAdapterApple::DoClipboardIsSupported() -> bool {
#if BA_XCODE_BUILD
return BallisticaKit::CocoaFromCpp::ClipboardIsSupported();
#else
return CorePlatform::DoClipboardIsSupported();
#endif
}

auto AppAdapterApple::DoClipboardHasText() -> bool {
#if BA_XCODE_BUILD
return BallisticaKit::CocoaFromCpp::ClipboardHasText();
#else
return CorePlatform::DoClipboardHasText();
#endif
}

void AppAdapterApple::DoClipboardSetText(const std::string& text) {
#if BA_XCODE_BUILD
BallisticaKit::CocoaFromCpp::ClipboardSetText(text);
#else
CorePlatform::DoClipboardSetText(text);
#endif
}

auto AppAdapterApple::DoClipboardGetText() -> std::string {
#if BA_XCODE_BUILD
auto contents = BallisticaKit::CocoaFromCpp::ClipboardGetText();
if (contents) {
return std::string(contents.get());
}
throw Exception("No text on clipboard.");
#else
return CorePlatform::DoClipboardGetText();
#endif
}

} // namespace ballistica::base

#endif // BA_XCODE_BUILD
4 changes: 4 additions & 0 deletions src/ballistica/base/app_adapter/app_adapter_apple.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class AppAdapterApple : public AppAdapter {
void SetHardwareCursorVisible(bool visible) override;
void TerminateApp() override;
void ApplyGraphicsSettings(const GraphicsSettings* settings) override;
auto DoClipboardIsSupported() -> bool override;
auto DoClipboardHasText() -> bool override;
void DoClipboardSetText(const std::string& text) override;
auto DoClipboardGetText() -> std::string override;

private:
class ScopedAllowGraphics_;
Expand Down
22 changes: 22 additions & 0 deletions src/ballistica/base/app_adapter/app_adapter_sdl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,28 @@ auto AppAdapterSDL::HasDirectKeyboardInput() -> bool {
return true;
}

auto AppAdapterSDL::DoClipboardIsSupported() -> bool { return true; }

auto AppAdapterSDL::DoClipboardHasText() -> bool {
return SDL_HasClipboardText();
}

void AppAdapterSDL::DoClipboardSetText(const std::string& text) {
SDL_SetClipboardText(text.c_str());
}

auto AppAdapterSDL::DoClipboardGetText() -> std::string {
// Go through SDL functionality on SDL based platforms;
// otherwise default to no clipboard.
char* out = SDL_GetClipboardText();
if (out == nullptr) {
throw Exception("Error fetching clipboard contents.", PyExcType::kRuntime);
}
std::string out_s{out};
SDL_free(out);
return out_s;
}

} // namespace ballistica::base

#endif // BA_SDL_BUILD
4 changes: 4 additions & 0 deletions src/ballistica/base/app_adapter/app_adapter_sdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class AppAdapterSDL : public AppAdapter {
auto InGraphicsContext() -> bool override;
void DoPushGraphicsContextRunnable(Runnable* runnable) override;
void CursorPositionForDraw(float* x, float* y) override;
auto DoClipboardIsSupported() -> bool override;
auto DoClipboardHasText() -> bool override;
void DoClipboardSetText(const std::string& text) override;
auto DoClipboardGetText() -> std::string override;

private:
class ScopedAllowGraphics_;
Expand Down
9 changes: 9 additions & 0 deletions src/ballistica/base/input/input.cc
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,15 @@ void Input::PushTextInputEvent(const std::string& text) {
return;
}

// Also ignore if there are any mod keys being held.
// We process some of our own keyboard shortcuts and don't
// want text input to come through at the same time.
if (keys_held_.contains(SDLK_LCTRL) || keys_held_.contains(SDLK_RCTRL)
|| keys_held_.contains(SDLK_LALT) || keys_held_.contains(SDLK_RALT)
|| keys_held_.contains(SDLK_LGUI) || keys_held_.contains(SDLK_RGUI)) {
return;
}

// We try to handle char filtering here (to keep it consistent across
// platforms) but make a stink if they sent us something that we can't
// at least translate to unicode.
Expand Down
3 changes: 2 additions & 1 deletion src/ballistica/base/platform/base_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class BasePlatform {
/// active/inactive.
virtual void LoginAdapterBackEndActiveChange(const std::string& login_type,
bool active);

#pragma mark MISC --------------------------------------------------------------

/// Do we define a platform-specific string editor? This is something like
Expand Down Expand Up @@ -115,8 +116,8 @@ class BasePlatform {
/// class versions can go here.
virtual void PostInit();

bool ran_base_post_init_ : 1 {};
PythonRef string_edit_adapter_{};
bool ran_base_post_init_{};
std::string public_device_uuid_;
};

Expand Down
9 changes: 5 additions & 4 deletions src/ballistica/base/python/methods/python_methods_misc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <list>
#include <unordered_map>

#include "ballistica/base/app_adapter/app_adapter.h"
#include "ballistica/base/assets/sound_asset.h"
#include "ballistica/base/input/input.h"
#include "ballistica/base/platform/base_platform.h"
Expand Down Expand Up @@ -128,7 +129,7 @@ static PyMethodDef PyHasTouchScreenDef = {

static auto PyClipboardIsSupported(PyObject* self) -> PyObject* {
BA_PYTHON_TRY;
if (g_core->platform->ClipboardIsSupported()) {
if (g_base->app_adapter->ClipboardIsSupported()) {
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
Expand All @@ -154,7 +155,7 @@ static PyMethodDef PyClipboardIsSupportedDef = {

static auto PyClipboardHasText(PyObject* self) -> PyObject* {
BA_PYTHON_TRY;
if (g_core->platform->ClipboardHasText()) {
if (g_base->app_adapter->ClipboardHasText()) {
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
Expand Down Expand Up @@ -187,7 +188,7 @@ static auto PyClipboardSetText(PyObject* self, PyObject* args, PyObject* keywds)
const_cast<char**>(kwlist), &value)) {
return nullptr;
}
g_core->platform->ClipboardSetText(value);
g_base->app_adapter->ClipboardSetText(value);
Py_RETURN_NONE;
BA_PYTHON_CATCH;
}
Expand All @@ -211,7 +212,7 @@ static PyMethodDef PyClipboardSetTextDef = {

static auto PyClipboardGetText(PyObject* self) -> PyObject* {
BA_PYTHON_TRY;
return PyUnicode_FromString(g_core->platform->ClipboardGetText().c_str());
return PyUnicode_FromString(g_base->app_adapter->ClipboardGetText().c_str());
Py_RETURN_FALSE;
BA_PYTHON_CATCH;
}
Expand Down
Loading

0 comments on commit f0ebe2b

Please sign in to comment.