Skip to content

Commit 4096889

Browse files
committed
Add option to disable native clipboard
This is related to aseprite#1100, as the Linux port is not well tested and may fail, it's good to have an option to disable the native clipboard code just in case.
1 parent 218a086 commit 4096889

13 files changed

+144
-22
lines changed

data/pref.xml

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
<option id="mini_font" type="std::string" migrate="Options.UserMiniFont" />
153153
</section>
154154
<section id="experimental" text="Experimental">
155+
<option id="use_native_clipboard" type="bool" default="true" />
155156
<option id="use_native_file_dialog" type="bool" default="false" />
156157
<option id="flash_layer" type="bool" default="false" migrate="Options.FlashLayer" />
157158
<option id="nonactive_layers_opacity" type="int" default="255" />

data/strings/en.ini

+1
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,7 @@ disable_extension = &Disable
10081008
uninstall_extension = &Uninstall
10091009
open_extension_folder = Open &Folder
10101010
user_interface = User Interface
1011+
native_clipboard = Use native clipboard
10111012
native_file_dialog = Use native file dialog
10121013
flash_selected_layer = Flash layer when it is selected
10131014
non_active_layer_opacity = Opacity for non-active layers:

data/widgets/options.xml

+1
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@
352352
<!-- Experimental -->
353353
<vbox id="section_experimental">
354354
<separator text="@.user_interface" horizontal="true" />
355+
<check id="native_clipboard" text="@.native_clipboard" />
355356
<check id="native_file_dialog" text="@.native_file_dialog" />
356357
<check id="flash_layer" text="@.flash_selected_layer" />
357358
<hbox>

src/app/app.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ void App::initialize(const AppOptions& options)
228228
if (isGui()) {
229229
LOG("APP: GUI mode\n");
230230

231+
// Set the ClipboardDelegate impl to copy/paste text in the native
232+
// clipboard from the ui::Entry control.
233+
m_uiSystem->setClipboardDelegate(&m_modules->m_clipboardManager);
234+
231235
// Setup the GUI cursor and redraw screen
232236
ui::set_use_native_cursors(preferences().cursor.useNativeCursor());
233237
ui::set_mouse_cursor_scale(preferences().cursor.cursorScale());

src/app/commands/cmd_options.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ class OptionsWindow : public app::gen::Options {
250250

251251
onNativeCursorChange();
252252

253+
if (m_pref.experimental.useNativeClipboard())
254+
nativeClipboard()->setSelected(true);
255+
253256
if (m_pref.experimental.useNativeFileDialog())
254257
nativeFileDialog()->setSelected(true);
255258

@@ -486,6 +489,7 @@ class OptionsWindow : public app::gen::Options {
486489
m_pref.undo.allowNonlinearHistory(undoAllowNonlinearHistory()->isSelected());
487490

488491
// Experimental features
492+
m_pref.experimental.useNativeClipboard(nativeClipboard()->isSelected());
489493
m_pref.experimental.useNativeFileDialog(nativeFileDialog()->isSelected());
490494
m_pref.experimental.flashLayer(flashLayer()->isSelected());
491495
m_pref.experimental.nonactiveLayersOpacity(nonactiveLayersOpacity()->getValue());

src/app/util/clipboard.cpp

+40-9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "app/modules/editors.h"
2222
#include "app/modules/gfx.h"
2323
#include "app/modules/gui.h"
24+
#include "app/pref/preferences.h"
2425
#include "app/transaction.h"
2526
#include "app/ui/color_bar.h"
2627
#include "app/ui/editor/editor.h"
@@ -31,6 +32,7 @@
3132
#include "app/util/clipboard_native.h"
3233
#include "app/util/new_image_from_mask.h"
3334
#include "base/shared_ptr.h"
35+
#include "clip/clip.h"
3436
#include "doc/doc.h"
3537
#include "render/ordered_dither.h"
3638
#include "render/quantization.h"
@@ -99,6 +101,16 @@ static ClipboardRange clipboard_range;
99101

100102
static ClipboardManager* g_instance = nullptr;
101103

104+
static bool use_native_clipboard()
105+
{
106+
return Preferences::instance().experimental.useNativeClipboard();
107+
}
108+
109+
ClipboardManager* ClipboardManager::instance()
110+
{
111+
return g_instance;
112+
}
113+
102114
ClipboardManager::ClipboardManager()
103115
{
104116
ASSERT(!g_instance);
@@ -123,9 +135,25 @@ ClipboardManager::~ClipboardManager()
123135
g_instance = nullptr;
124136
}
125137

126-
ClipboardManager* ClipboardManager::instance()
138+
void ClipboardManager::setClipboardText(const std::string& text)
127139
{
128-
return g_instance;
140+
if (use_native_clipboard()) {
141+
clip::set_text(text);
142+
}
143+
else {
144+
m_text = text;
145+
}
146+
}
147+
148+
bool ClipboardManager::getClipboardText(std::string& text)
149+
{
150+
if (use_native_clipboard()) {
151+
return clip::get_text(text);
152+
}
153+
else {
154+
text = m_text;
155+
return true;
156+
}
129157
}
130158

131159
static void set_clipboard_image(Image* image,
@@ -148,7 +176,8 @@ static void set_clipboard_image(Image* image,
148176
image->setMaskColor(-1);
149177
}
150178

151-
set_native_clipboard_bitmap(image, mask, palette);
179+
if (use_native_clipboard())
180+
set_native_clipboard_bitmap(image, mask, palette);
152181

153182
if (image && !image_source_is_transparent)
154183
image->setMaskColor(oldMask);
@@ -181,7 +210,8 @@ static bool copy_from_document(const Site& site, bool merged = false)
181210
ClipboardFormat get_current_format()
182211
{
183212
// Check if the native clipboard has an image
184-
if (has_native_clipboard_bitmap())
213+
if (use_native_clipboard() &&
214+
has_native_clipboard_bitmap())
185215
return ClipboardImage;
186216
else if (clipboard_image)
187217
return ClipboardImage;
@@ -302,8 +332,8 @@ void paste()
302332
switch (get_current_format()) {
303333

304334
case clipboard::ClipboardImage: {
305-
// Get the image from the clipboard.
306-
{
335+
// Get the image from the native clipboard.
336+
if (use_native_clipboard()) {
307337
Image* native_image = nullptr;
308338
Mask* native_mask = nullptr;
309339
Palette* native_palette = nullptr;
@@ -567,15 +597,16 @@ void paste()
567597
bool get_image_size(gfx::Size& size)
568598
{
569599
#if defined(_WIN32) || defined(__APPLE__)
570-
if (get_native_clipboard_bitmap_size(&size))
600+
if (use_native_clipboard() &&
601+
get_native_clipboard_bitmap_size(&size))
571602
return true;
572-
#else
603+
#endif
604+
573605
if (clipboard_image) {
574606
size.w = clipboard_image->width();
575607
size.h = clipboard_image->height();
576608
return true;
577609
}
578-
#endif
579610

580611
return false;
581612
}

src/app/util/clipboard.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Aseprite
2-
// Copyright (C) 2001-2016 David Capello
2+
// Copyright (C) 2001-2018 David Capello
33
//
44
// This program is distributed under the terms of
55
// the End-User License Agreement for Aseprite.
@@ -11,6 +11,7 @@
1111
#include "gfx/point.h"
1212
#include "gfx/size.h"
1313
#include "ui/base.h"
14+
#include "ui/clipboard_delegate.h"
1415

1516
namespace doc {
1617
class Image;
@@ -37,12 +38,17 @@ namespace app {
3738

3839
// TODO Horrible API: refactor it (maybe a merge with she::clipboard).
3940

40-
class ClipboardManager {
41+
class ClipboardManager : public ui::ClipboardDelegate {
4142
public:
43+
static ClipboardManager* instance();
44+
4245
ClipboardManager();
4346
~ClipboardManager();
4447

45-
static ClipboardManager* instance();
48+
void setClipboardText(const std::string& text) override;
49+
bool getClipboardText(std::string& text) override;
50+
private:
51+
std::string m_text; // Text used when the native clipboard is disabled
4652
};
4753

4854
ClipboardFormat get_current_format();

src/ui/CMakeLists.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Aseprite UI Library
2-
# Copyright (C) 2001-2017 David Capello
2+
# Copyright (C) 2001-2018 David Capello
33

44
if(WIN32)
55
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
@@ -57,7 +57,6 @@ add_library(ui-lib
5757

5858
target_link_libraries(ui-lib
5959
she
60-
clip
6160
gfx-lib
6261
laf-base
6362
obs

src/ui/clipboard_delegate.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Aseprite UI Library
2+
// Copyright (C) 2018 David Capello
3+
//
4+
// This file is released under the terms of the MIT license.
5+
// Read LICENSE.txt for more information.
6+
7+
#ifndef UI_CLIPBOARD_DELEGATE_H_INCLUDED
8+
#define UI_CLIPBOARD_DELEGATE_H_INCLUDED
9+
#pragma once
10+
11+
#include <string>
12+
13+
namespace ui {
14+
15+
class ClipboardDelegate {
16+
public:
17+
virtual ~ClipboardDelegate() { }
18+
virtual void setClipboardText(const std::string& text) = 0;
19+
virtual bool getClipboardText(std::string& text) = 0;
20+
};
21+
22+
} // namespace ui
23+
24+
#endif

src/ui/entry.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Aseprite UI Library
2-
// Copyright (C) 2001-2017 David Capello
2+
// Copyright (C) 2001-2018 David Capello
33
//
44
// This file is released under the terms of the MIT license.
55
// Read LICENSE.txt for more information.
@@ -12,7 +12,6 @@
1212

1313
#include "base/bind.h"
1414
#include "base/string.h"
15-
#include "clip/clip.h"
1615
#include "she/draw_text.h"
1716
#include "she/font.h"
1817
#include "she/system.h"
@@ -636,7 +635,7 @@ void Entry::executeCmd(EntryCmd cmd, int unicodeChar, bool shift_pressed)
636635
if (selbeg >= 0) {
637636
// *cut* text!
638637
if (cmd == EntryCmd::Cut)
639-
clip::set_text(selectedText());
638+
set_clipboard_text(selectedText());
640639

641640
// remove text
642641
text.erase(m_boxes[selbeg].from,
@@ -656,7 +655,7 @@ void Entry::executeCmd(EntryCmd cmd, int unicodeChar, bool shift_pressed)
656655

657656
case EntryCmd::Paste: {
658657
std::string clipboard;
659-
if (clip::get_text(clipboard)) {
658+
if (get_clipboard_text(clipboard)) {
660659
// delete the entire selection
661660
if (selbeg >= 0) {
662661
text.erase(m_boxes[selbeg].from,
@@ -688,7 +687,7 @@ void Entry::executeCmd(EntryCmd cmd, int unicodeChar, bool shift_pressed)
688687

689688
case EntryCmd::Copy:
690689
if (selbeg >= 0)
691-
clip::set_text(selectedText());
690+
set_clipboard_text(selectedText());
692691
break;
693692

694693
case EntryCmd::DeleteBackward:

src/ui/system.cpp

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Aseprite UI Library
2-
// Copyright (C) 2001-2017 David Capello
2+
// Copyright (C) 2001-2018 David Capello
33
//
44
// This file is released under the terms of the MIT license.
55
// Read LICENSE.txt for more information.
@@ -15,6 +15,7 @@
1515
#include "she/display.h"
1616
#include "she/surface.h"
1717
#include "she/system.h"
18+
#include "ui/clipboard_delegate.h"
1819
#include "ui/cursor.h"
1920
#include "ui/intern.h"
2021
#include "ui/intern.h"
@@ -171,8 +172,20 @@ static void update_mouse_cursor()
171172
}
172173
}
173174

175+
// static
176+
static UISystem* g_instance = nullptr;
177+
178+
UISystem* UISystem::instance()
179+
{
180+
return g_instance;
181+
}
182+
174183
UISystem::UISystem()
184+
: m_clipboardDelegate(nullptr)
175185
{
186+
ASSERT(!g_instance);
187+
g_instance = this;
188+
176189
main_gui_thread = base::this_thread::native_handle();
177190
mouse_cursor_type = kOutsideDisplay;
178191
support_native_custom_cursor =
@@ -196,6 +209,9 @@ UISystem::~UISystem()
196209
_internal_set_mouse_display(nullptr);
197210
if (!update_custom_native_cursor(nullptr))
198211
update_mouse_overlay(nullptr);
212+
213+
ASSERT(g_instance == this);
214+
g_instance = nullptr;
199215
}
200216

201217
void _internal_set_mouse_display(she::Display* display)
@@ -223,6 +239,24 @@ int display_h()
223239
return 1;
224240
}
225241

242+
void set_clipboard_text(const std::string& text)
243+
{
244+
ASSERT(g_instance);
245+
ClipboardDelegate* delegate = g_instance->clipboardDelegate();
246+
if (delegate)
247+
delegate->setClipboardText(text);
248+
}
249+
250+
bool get_clipboard_text(std::string& text)
251+
{
252+
ASSERT(g_instance);
253+
ClipboardDelegate* delegate = g_instance->clipboardDelegate();
254+
if (delegate)
255+
return delegate->getClipboardText(text);
256+
else
257+
return false;
258+
}
259+
226260
void update_cursor_overlay()
227261
{
228262
if (mouse_cursor_overlay != NULL && mouse_scares == 0) {

src/ui/system.h

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Aseprite UI Library
2-
// Copyright (C) 2001-2017 David Capello
2+
// Copyright (C) 2001-2018 David Capello
33
//
44
// This file is released under the terms of the MIT license.
55
// Read LICENSE.txt for more information.
@@ -13,22 +13,39 @@
1313
#include "ui/cursor_type.h"
1414
#include "ui/mouse_buttons.h"
1515

16+
#include <string>
17+
1618
namespace she { class Display; }
1719

1820
namespace ui {
1921

22+
class ClipboardDelegate;
2023
class Cursor;
2124
class Widget;
2225

2326
class UISystem {
2427
public:
28+
static UISystem* instance();
29+
2530
UISystem();
2631
~UISystem();
32+
33+
void setClipboardDelegate(ClipboardDelegate* delegate) {
34+
m_clipboardDelegate = delegate;
35+
}
36+
ClipboardDelegate* clipboardDelegate() {
37+
return m_clipboardDelegate;
38+
}
39+
private:
40+
ClipboardDelegate* m_clipboardDelegate;
2741
};
2842

2943
int display_w();
3044
int display_h();
3145

46+
void set_clipboard_text(const std::string& text);
47+
bool get_clipboard_text(std::string& text);
48+
3249
// Mouse related
3350

3451
// Updates the position of the mouse cursor overlay depending on the

0 commit comments

Comments
 (0)