1+ library ;
2+
3+ import 'dart:async' ;
4+
5+ import 'addons.dart' ;
6+ import 'main.dart' ;
7+ import 'src/theme.dart' ;
8+ import 'package:flutter/material.dart' ;
9+ import 'package:styled_logger/styled_logger.dart' ;
10+
11+ export 'src/widgetframework.dart' ;
12+ export 'src/menubar.dart' show DictionariesMenuBarEntry;
13+ export 'src/nodes.dart' show RootNode;
14+ export 'src/theme.dart' show DictionariesTheme;
15+
16+ /// This class is what you base your entire addon around.
17+ /// You will make a new class that inherits this class and provides the info and permissions.
18+ abstract class DictionariesAddon {
19+ /// The name of your addon.
20+ final String name;
21+
22+ /// The ID of your addon. This should follow the Java naming procedure, and the final identifier should be lower snake case. Example:
23+ ///
24+ /// `com.calebh101.oc_snapshot`
25+ final String id;
26+
27+ /// The version of your addon. This can be in any format you like.
28+ final String version;
29+
30+ /// The optional short description of your addon.
31+ final String ? description;
32+
33+ /// A list of names of who made this, This can be in any format you like.
34+ final List <String > authors;
35+
36+ /// A link to your addon's optional "main page". Take this how you'd like, but [repository] is a separate property for an addon's repository.
37+ final Uri ? mainpage;
38+
39+ /// An addon's open source repository.
40+ ///
41+ /// Reminder: Your addon must be open-source and publicly available to be able to be used in Dictionaries!
42+ final Uri ? repository;
43+
44+ /// If this addon should be completely ignored by the loader.
45+ final bool doNotShow;
46+
47+ /// This class is what you base your entire addon around.
48+ /// You will make a new class that inherits this class and provides the info and permissions.
49+ DictionariesAddon ({
50+ required this .name,
51+ required this .id,
52+ required this .version,
53+ this .authors = const [],
54+ this .description,
55+ this .mainpage,
56+ this .repository,
57+ this .doNotShow = false ,
58+ });
59+
60+ /// This is called to register your addon.
61+ /// **Do not call this yourself**.
62+ FutureOr <void > register (bool debug) async {
63+ injectedAddons.add (this );
64+ await onRegister (debug);
65+ }
66+
67+ /// This is called when your addon is registered. You don't call this, but you *override* this in your class.
68+ ///
69+ /// You should put all injections you do and/or background services you start in here.
70+ FutureOr <void > onRegister (bool debug) {}
71+
72+ /// This is called when your addon is disengaged. You should stop all background processes when this is called.
73+ void onDisengage () {}
74+
75+ bool get doShow => ! doNotShow;
76+
77+ /// Get your addon's current [AddonContext] . This is used for identification in injections.
78+ AddonContext get context => AddonContext .fromId (id);
79+
80+ /// Get your addon's current [AddonContext] . This is used for identification in injections.
81+ AddonContext get addonContext => context;
82+ }
83+
84+ abstract class DictionariesUIInjection {
85+ const DictionariesUIInjection ();
86+
87+ void inject (AddonContext context) {
88+ injectedAddonUIs.add ((this , context));
89+ }
90+ }
91+
92+ enum DictionariesWidgetInjectionTarget {
93+ rootNode,
94+ }
95+
96+ final class DictionariesWidgetInjection <T extends DictionariesWidget > extends DictionariesUIInjection {
97+ final DictionariesWidgetInjectionTarget target;
98+ final DictionariesWidget Function (BuildContext context, T widget) _build;
99+
100+ DictionariesWidget build (BuildContext context, DictionariesWidget widget) {
101+ if (widget is T ) {
102+ return _build (context, widget);
103+ } else {
104+ Logger .warn ("Detected type mismatch when building ${this .runtimeType }." );
105+ return widget;
106+ }
107+ }
108+
109+ const DictionariesWidgetInjection ({required this .target, required DictionariesWidget Function (BuildContext context, T widget) build}) : _build = build;
110+ }
111+
112+ final class DictionariesMaterialAppInjection extends DictionariesUIInjection {
113+ final MaterialApp Function (BuildContext context, MaterialApp widget) build;
114+
115+ const DictionariesMaterialAppInjection ({required this .build});
116+
117+ void inject (AddonContext context) {
118+ injectedAddonUIs.add ((this , context));
119+ }
120+ }
121+
122+ class DictionariesMenuBarInjection {
123+ final List <String > keys;
124+ final List <DictionariesMenuBarEntry > entries;
125+ final DictionariesMenuBarPosition ? rightAfter;
126+
127+ const DictionariesMenuBarInjection (this .keys, this .entries, {this .rightAfter});
128+
129+ void inject (AddonContext context) {
130+ injectedMenuEntries.add ((this , context));
131+ }
132+ }
133+
134+ class DictionariesMenuBarDeletion {
135+ final List <String > keys;
136+ const DictionariesMenuBarDeletion (this .keys);
137+
138+ void inject (AddonContext context) {
139+ injectedMenuDeletions.add ((this , context));
140+ }
141+ }
142+
143+ class DictionariesMenuBarPosition {
144+ final bool leading;
145+ final String ? text;
146+
147+ const DictionariesMenuBarPosition .fromKey (String key) : text = key, leading = false ;
148+ const DictionariesMenuBarPosition .leading () : text = null , leading = true ;
149+ }
150+
151+ class AddonLogger {
152+ static void print (AddonContext context, Object ? input, {List <Object ?>? attachments}) {
153+ Logger .print ("${context .id }: $input " , attachments: attachments);
154+ }
155+
156+ static void warn (AddonContext context, Object ? input, {List <Object ?>? attachments}) {
157+ Logger .warn ("Addon: $input " , attachments: attachments);
158+ }
159+
160+ static void error (AddonContext context, Object ? input, {List <Object ?>? attachments}) {
161+ Logger .error ("Addon: $input " , attachments: attachments);
162+ }
163+ }
164+
165+ class AddonContext {
166+ final String id;
167+ const AddonContext .fromId (this .id);
168+ }
169+
170+ extension DictionariesThemeInjection on DictionariesTheme {
171+ void inject (AddonContext context, String id) {
172+ addInjectedTheme ("${context .id }.$id " , context.id, this );
173+ }
174+ }
0 commit comments