Skip to content

Commit 35e56d3

Browse files
committed
- add clipboard api
1 parent f57a086 commit 35e56d3

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

core/pen/include/os.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ namespace pen
5757
bool os_is_backgrounded();
5858
void os_register_background_callback(void (*callback)(bool));
5959
bool os_require_audio_reinit(bool reset);
60+
Str os_get_clipboard_string();
61+
void os_clear_clipboard_string();
62+
void os_enable_paste_popup(bool enable);
6063

6164
// music
6265
struct music_item

core/pen/source/ios/os.mm

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ - (instancetype)initWithView:(nonnull MTKView*)view;
4444
@end
4545

4646
@interface pen_view_controller : UIViewController
47+
@property(strong, nonatomic) MTKView* mtk_view;
48+
4749
- (void)viewWasDoubleTapped:(id)sender;
4850
- (BOOL)prefersHomeIndicatorAutoHidden;
4951
@end
@@ -86,6 +88,8 @@ -(MPRemoteCommandHandlerStatus)like;
8688
void (*background_callback)(bool) = nullptr;
8789
bool background_audio = true;
8890
bool require_audio_reinit = false;
91+
Str clipboard = "";
92+
bool enable_paste_popup = false;
8993
};
9094
os_context s_context;
9195

@@ -162,6 +166,7 @@ - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(N
162166

163167
// create view controller
164168
self.view_controller = [[pen_view_controller alloc] initWithNibName:nil bundle:nil];
169+
self.view_controller.mtk_view = self.mtk_view;
165170

166171
// hook up
167172
[self.view_controller setView:self.mtk_view];
@@ -170,6 +175,10 @@ - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(N
170175

171176
// enable support for osk input
172177
pen::os_init_on_screen_keyboard();
178+
179+
// long press gesture for paste menu
180+
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self.view_controller action:@selector(handleLongPress:)];
181+
[self.mtk_view addGestureRecognizer:longPress];
173182

174183
return YES;
175184
}
@@ -354,6 +363,51 @@ - (void)touchesCancelled:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event
354363
{
355364
[self handleTouch:touches withEvent:event];
356365
}
366+
367+
- (UIRectEdge)preferredScreenEdgesDeferringSystemGestures {
368+
// Choose the edges you want to defer
369+
return UIRectEdgeAll; // or UIRectEdgeBottom, etc.
370+
}
371+
372+
- (void)viewDidAppear:(BOOL)animated {
373+
[super viewDidAppear:animated];
374+
[self setNeedsUpdateOfScreenEdgesDeferringSystemGestures];
375+
}
376+
377+
- (BOOL)canBecomeFirstResponder {
378+
return YES;
379+
}
380+
381+
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
382+
if (action == @selector(paste:)) {
383+
// Only enable if there is something to paste
384+
return [UIPasteboard generalPasteboard].hasStrings;
385+
}
386+
return NO;
387+
}
388+
389+
- (void)paste:(id)sender {
390+
UIPasteboard *pb = [UIPasteboard generalPasteboard];
391+
NSString *text = pb.string;
392+
s_context.clipboard = text.UTF8String;
393+
}
394+
395+
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture
396+
{
397+
// prevent popup
398+
if (!s_context.enable_paste_popup || gesture.state != UIGestureRecognizerStateBegan)
399+
return;
400+
401+
// Make the VC first responder
402+
[self becomeFirstResponder];
403+
404+
CGPoint p = [gesture locationInView:self.mtk_view];
405+
CGRect targetRect = CGRectMake(p.x, p.y, 1, 1);
406+
407+
UIMenuController *menu = [UIMenuController sharedMenuController];
408+
[menu showMenuFromView:self.mtk_view rect:targetRect];
409+
}
410+
357411
@end
358412

359413
@implementation pen_text_field_delegate
@@ -859,4 +913,19 @@ bool os_require_audio_reinit(bool reset)
859913

860914
return res;
861915
}
916+
917+
Str os_get_clipboard_string()
918+
{
919+
return s_context.clipboard;
920+
}
921+
922+
void os_clear_clipboard_string()
923+
{
924+
s_context.clipboard = "";
925+
}
926+
927+
void os_enable_paste_popup(bool enable)
928+
{
929+
s_context.enable_paste_popup = enable;
930+
}
862931
}

0 commit comments

Comments
 (0)