Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Application::copyToClipboard for iOS #16

Merged
merged 4 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/docs/reference/foundation/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ namespace bdn {

Opens the given URL in a suitable external application. Web URLs will be opened in the system's standard web browser. Application-specific URLs will open in the respective application.

* **virtual void copyToClipboard(const String &str)**

Copies the given string to the clipboard.

## Resources

* **virtual String uriToBundledFileUri(const String &uri)**
Expand Down
4 changes: 3 additions & 1 deletion examples/BodenDemo/src/BodenDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "FontPage.h"
#include "ImagesPage.h"
#include "ListViewPage.h"
#include "MiscPage.h"
#include "PropertiesPage.h"
#include "StyledTextPage.h"
#include "TimersPage.h"
Expand All @@ -23,7 +24,7 @@ using namespace bdn;
class PagesDataSource : public ListViewDataSource
{
public:
std::array<std::pair<String, std::function<std::shared_ptr<View>()>>, 10> pages = {
std::array<std::pair<String, std::function<std::shared_ptr<View>()>>, 11> pages = {
std::make_pair("UI Demo", [=]() { return std::make_shared<UIDemoPage>(needsInit, _window); }),
std::make_pair("Timer demo", [=]() { return std::make_shared<TimersPage>(needsInit); }),
std::make_pair("WebView demo", [=]() { return std::make_shared<WebViewPage>(needsInit); }),
Expand All @@ -34,6 +35,7 @@ class PagesDataSource : public ListViewDataSource
std::make_pair("Fonts", [=]() { return std::make_shared<FontPage>(needsInit); }),
std::make_pair("Styled Text", [=]() { return std::make_shared<StyledTextPage>(needsInit); }),
std::make_pair("Focus", [=]() { return std::make_shared<FocusPage>(needsInit); }),
std::make_pair("Misc", [=]() { return std::make_shared<MiscPage>(needsInit); }),
};

public:
Expand Down
21 changes: 21 additions & 0 deletions examples/BodenDemo/src/MiscPage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <bdn/Application.h>
#include <bdn/Json.h>
#include <bdn/ui/Button.h>
#include <bdn/ui/ContainerView.h>
#include <bdn/ui/yoga/FlexStylesheet.h>

#include "MiscPage.h"
#include <bdn/log.h>

namespace bdn
{
void MiscPage::init()
{
stylesheet = FlexJsonStringify({"flexGrow" : 1.0});

auto buttonCopy = std::make_shared<Button>();
buttonCopy->label = "Hello clipboard!";
buttonCopy->onClick() += [](auto) { App()->copyToClipboard("Hello clipboard!"); };
addChildView(makeRow("Clipboard", buttonCopy, 0.));
}
}
13 changes: 13 additions & 0 deletions examples/BodenDemo/src/MiscPage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "Page.h"

namespace bdn
{
class MiscPage : public ui::CoreLess<ContainerView>
{
public:
using CoreLess<ContainerView>::CoreLess;
void init() override;
};
}
1 change: 1 addition & 0 deletions framework/foundation/include/bdn/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace bdn
public:
virtual void openURL(const String &url) = 0;
virtual String uriToBundledFileUri(const String &uri) { return uri; }
virtual void copyToClipboard(const String &str) = 0;

protected:
virtual void platformSpecificInit() {}
Expand Down
2 changes: 2 additions & 0 deletions framework/foundation/include/bdn/GenericApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ namespace bdn

void openURL(const String &url) override {}

void copyToClipboard(const String &str) override {}

protected:
virtual bool shouldExit() const
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace bdn::ui::android
void initiateExitIfPossible(int exitCode) override;
void openURL(const String &url) override;
String uriToBundledFileUri(const String &uri) override;
void copyToClipboard(const String &str) override;

protected:
void disposeMainDispatcher() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ namespace bdn::android::wrapper
constexpr static const JTObject<kNativeRootActivityClassName>::StaticMethod<String(String)>
getResourceURIFromURI{"getResourceURIFromURI"};

constexpr static const JTObject<kNativeRootActivityClassName>::StaticMethod<void(String)> copyToClipboard{
"copyToClipboard"};

// JavaMethod<void(java::wrapper::CharSequence)> setTitle{this, "setTitle"};
// JavaMethod<void(bool)> enableBackButton{this, "enableBackButton"};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.boden.android.NativeInit;
import io.boden.android.NativeRootView;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
Expand Down Expand Up @@ -292,6 +293,12 @@ public static final String getResourceURIFromURI(String uri) {
return uri;
}

public static final void copyToClipboard(String str)
{
ClipboardManager clipboard = (ClipboardManager)getRootActivity().getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(str);
}

@Override
public void onConfigurationChanged (Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Expand Down
5 changes: 5 additions & 0 deletions framework/ui/platforms/android/src/UIApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ namespace bdn::ui::android
{
return bdn::android::wrapper::NativeRootActivity::getResourceURIFromURI(uri);
}

void UIApplication::copyToClipboard(const String &str)
{
return bdn::android::wrapper::NativeRootActivity::copyToClipboard(str);
}
}

extern "C" JNIEXPORT void JNICALL Java_io_boden_android_NativeInit_nativeDestroy(JNIEnv *env, jclass cls,
Expand Down
2 changes: 2 additions & 0 deletions framework/ui/platforms/ios/include/bdn/ios/UIApplication.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace bdn::ui::ios

String uriToBundledFileUri(const String &uri) override;

void copyToClipboard(const String &str) override;

public:
bool _applicationWillFinishLaunching(NSDictionary *launchOptions);
bool _applicationDidFinishLaunching(NSDictionary *launchOptions);
Expand Down
6 changes: 6 additions & 0 deletions framework/ui/platforms/ios/src/UIApplication.mm
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ - (void)applicationWillTerminate:(UIApplication *)application
return "file:///" + result;
}

void UIApplication::copyToClipboard(const String &str)
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = [NSString stringWithUTF8String:str.c_str()];
}

bool UIApplication::_applicationWillFinishLaunching(NSDictionary *launchOptions)
{
bdn::platformEntryWrapper(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace bdn::ui::mac
void disposeMainDispatcher() override;
void openURL(const String &url) override;
String uriToBundledFileUri(const String &uri) override;
void copyToClipboard(const String &str) override;

public:
void _applicationWillFinishLaunching(NSNotification *notification);
Expand Down
7 changes: 7 additions & 0 deletions framework/ui/platforms/mac/src/UIApplication.mm
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ - (void)applicationWillTerminate:(NSNotification *)aNotification
return "file:///" + result;
}

void UIApplication::copyToClipboard(const String &str)
{
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard declareTypes:[NSArray arrayWithObjects:NSPasteboardTypeString, nil] owner:nil];
[pasteboard setString:[NSString stringWithUTF8String:str.c_str()] forType:NSPasteboardTypeString];
}

void UIApplication::_applicationWillFinishLaunching(NSNotification *notification)
{
bdn::platformEntryWrapper(
Expand Down