-
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathcastleopendocument.pas
More file actions
156 lines (120 loc) · 4.99 KB
/
castleopendocument.pas
File metadata and controls
156 lines (120 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
{
Copyright 2012-2025 Michalis Kamburelis and Lazarus developers.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
Parts of this file are based on Lazarus LCL code, which has
exactly the same license as our "Castle Game Engine":
LGPL with static linking exception, see COPYING.txt for details.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{ Opening files and URLs. }
unit CastleOpenDocument;
{$include castleconf.inc}
{$define read_unit_compiler_directives}
{$I castleopendocument_open.inc}
{$undef read_unit_compiler_directives}
interface
resourcestring
SCannotOpenUrl = 'Browser not found on your system.';
{ Open URL with the suitable application.
This detects and handles also local files (as filenames, or URLs with "file:"
protocol).
@unorderedList(
@item(
On Android and iOS, it uses the OS functions to open the URL,
supporting all URL types that are handled by the installed applications.
For example, it will support the market:// URLs on Android.
)
@item(
On the @url(https://castle-engine.io/web web), we open the URL
by navigating to using
@url(https://developer.mozilla.org/en-US/docs/Web/API/Window/open window.open).
)
@item(
On desktops, it opens the URL using the default browser.
)
)
}
function OpenUrl(AUrl: String): Boolean;
{ Open a local file or directory.
@deprecated You should instead use OpenUrl,
that automatically detects local filenames and URLs leading to local filenames. }
function OpenDocument(APath: String): Boolean;
{ Share a text/link through user-choosen application.
This works only on Android and iOS right now.
@param(Title The short title of the share.)
@param(Subject Used as an email subject, and any other app on Android
that interprets EXTRA_SUBJECT parameter.)
@param(Content Multi-line share text content, possibly with URL inside.)
}
procedure ShareText(const Title, Subject, Content: string);
{ Show the application in the application store (Google Play on Android,
AppStore on iOS). Ignored on other platforms now.
@unorderedList(
@itemSpacing Compact
@item(On Android, ApplicationId should be the qualitied name of the application
(same thing you use as qualified_name in CastleEngineManifest.xml).
)
@item(On iOS, ApplicationId has to be the "Apple ID" number of your application
(you can see it e.g. in https://itunesconnect.apple.com/ page of your application).
)
) }
procedure OpenApplicationStore(const ApplicationId: string);
{ Vibrate the device.
Available on Android, iOS and Nintendo Switch now. Ignored on other platforms.
To include the necessary integration code in your Android project,
add the "vibrate" service inside CastleEngineManifest.xml,
see https://castle-engine.io/android_services . }
procedure Vibrate(const Miliseconds: Cardinal);
{ Simple on-screen notification using Android "toast" call.
This is available only on Android right now, ignored elsewhere. }
procedure OnScreenNotification(const Message: string);
deprecated 'This is Android-specific and probably will not be ever supported on other platforms. Better use CGE UI to make cros-platform UI notifications, like TCastleNotifications or just TCastleLabel with animated color/background.';
implementation
uses
SysUtils, Classes,
{$define read_uses}
{$I castleopendocument_open.inc}
{$undef read_uses}
CastleUriUtils, CastleUtils, CastleFilesUtils, CastleLog, CastleMessaging;
{ Has URL any anchor at the end, like "index.html#chapter1".
For such URLs, converting them to local filename may be possible
but is lossy: anchor would be lost.
Testcase: API docs to
file:///..../doc/reference/CastleSoundEngine.TCastleSound.html#DefaultReferenceDistance
would effectively open
/..../doc/reference/CastleSoundEngine.TCastleSound.html
(anchor lost). }
function UrlHasAnchor(const Url: String): Boolean;
var
U, Anchor: String;
begin
U := Url;
URIExtractAnchor(U, Anchor);
Result := Anchor <> '';
end;
{$define read_implementation}
{$I castleopendocument_open.inc}
{$undef read_implementation}
procedure ShareText(const Title, Subject, Content: string);
begin
Messaging.Send(['share-text', Title, Subject, Content]);
end;
procedure OpenApplicationStore(const ApplicationId: string);
begin
{$ifdef ANDROID} OpenUrl('market://details?id=' + ApplicationId); {$endif}
{$ifdef CASTLE_IOS} Messaging.Send(['open-application-store', ApplicationId]); {$endif}
end;
procedure Vibrate(const Miliseconds: Cardinal);
begin
Messaging.Send(['vibrate', IntToStr(Miliseconds)]);
end;
procedure OnScreenNotification(const Message: string);
begin
Messaging.Send(['on-screen-notification', Message]);
end;
end.