Initial commit

This commit is contained in:
wgh19
2024-05-13 09:36:23 +08:00
commit b095643cbc
160 changed files with 9956 additions and 0 deletions

41
lib/utils/app_links.dart Normal file
View File

@@ -0,0 +1,41 @@
import 'dart:io';
import 'package:app_links/app_links.dart';
import 'package:pixes/foundation/app.dart';
import 'package:pixes/foundation/log.dart';
import 'package:win32_registry/win32_registry.dart';
Future<void> _register(String scheme) async {
String appPath = Platform.resolvedExecutable;
String protocolRegKey = 'Software\\Classes\\$scheme';
RegistryValue protocolRegValue = const RegistryValue(
'URL Protocol',
RegistryValueType.string,
'',
);
String protocolCmdRegKey = 'shell\\open\\command';
RegistryValue protocolCmdRegValue = RegistryValue(
'',
RegistryValueType.string,
'"$appPath" "%1"',
);
final regKey = Registry.currentUser.createKey(protocolRegKey);
regKey.createValue(protocolRegValue);
regKey.createKey(protocolCmdRegKey).createValue(protocolCmdRegValue);
}
bool Function(Uri uri)? onLink;
void handleLinks() async {
if (App.isWindows) {
await _register("pixiv");
}
AppLinks().uriLinkStream.listen((uri) {
Log.info("App Link", uri.toString());
if (onLink?.call(uri) == true) {
return;
}
});
}

86
lib/utils/ext.dart Normal file
View File

@@ -0,0 +1,86 @@
extension ListExt<T> on List<T>{
/// Remove all blank value and return the list.
List<T> getNoBlankList(){
List<T> newList = [];
for(var value in this){
if(value.toString() != ""){
newList.add(value);
}
}
return newList;
}
T? firstWhereOrNull(bool Function(T element) test){
for(var element in this){
if(test(element)){
return element;
}
}
return null;
}
void addIfNotNull(T? value){
if(value != null){
add(value);
}
}
}
extension StringExt on String{
///Remove all value that would display blank on the screen.
String get removeAllBlank => replaceAll("\n", "").replaceAll(" ", "").replaceAll("\t", "");
/// convert this to a one-element list.
List<String> toList() => [this];
String _nums(){
String res = "";
for(int i=0; i<length; i++){
res += this[i].isNum?this[i]:"";
}
return res;
}
String get nums => _nums();
String setValueAt(String value, int index){
return replaceRange(index, index+1, value);
}
String? subStringOrNull(int start, [int? end]){
if(start < 0 || (end != null && end > length)){
return null;
}
return substring(start, end);
}
String replaceLast(String from, String to) {
if (isEmpty || from.isEmpty) {
return this;
}
final lastIndex = lastIndexOf(from);
if (lastIndex == -1) {
return this;
}
final before = substring(0, lastIndex);
final after = substring(lastIndex + from.length);
return '$before$to$after';
}
static bool hasMatch(String? value, String pattern) {
return (value == null) ? false : RegExp(pattern).hasMatch(value);
}
bool _isURL(){
final regex = RegExp(
r'^((http|https|ftp)://)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-|]*[\w@?^=%&/~+#-])?$',
caseSensitive: false);
return regex.hasMatch(this);
}
bool get isURL => _isURL();
bool get isNum => double.tryParse(this) != null;
}

22
lib/utils/io.dart Normal file
View File

@@ -0,0 +1,22 @@
import 'dart:io';
extension FSExt on FileSystemEntity {
Future<void> deleteIfExists() async {
if (await exists()) {
await delete();
}
}
int get size {
if (this is File) {
return (this as File).lengthSync();
} else if(this is Directory){
var size = 0;
for(var file in (this as Directory).listSync()){
size += file.size;
}
return size;
}
return 0;
}
}

View File

@@ -0,0 +1,26 @@
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import '../foundation/app.dart';
void mouseSideButtonCallback(GlobalKey<NavigatorState> key){
if(App.rootNavigatorKey.currentState?.canPop() ?? false) {
App.rootNavigatorKey.currentState?.pop();
return;
}
if(key.currentState?.canPop() ?? false){
key.currentState?.pop();
}
}
///监听鼠标侧键, 若为下键, 则调用返回
void listenMouseSideButtonToBack(GlobalKey<NavigatorState> key) async{
if(!App.isWindows){
return;
}
const channel = EventChannel("pixes/mouse");
await for(var res in channel.receiveBroadcastStream()){
if(res == 0){
mouseSideButtonCallback(key);
}
}
}

View File

@@ -0,0 +1,14 @@
import 'package:pixes/foundation/app.dart';
extension Translation on String {
String get tl {
var locale = App.locale;
return translation["${locale.languageCode}_${locale.countryCode}"]?[this] ??
this;
}
static const translation = <String, Map<String, String>>{
"zh_CN": {},
"zh_TW": {},
};
}