Skip to content

Commit

Permalink
Optimize proxies page
Browse files Browse the repository at this point in the history
Fix ua issues

Optimize more details
  • Loading branch information
chen08209 committed Jul 25, 2024
1 parent c36df8c commit 6de89d7
Show file tree
Hide file tree
Showing 44 changed files with 1,575 additions and 636 deletions.
1 change: 1 addition & 0 deletions core/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ func applyConfig() {
if configParams.IsPatch {
patchConfig(cfg.General)
} else {
closeConnections()
runtime.GC()
hub.UltraApplyConfig(cfg, true)
patchSelectGroup()
Expand Down
13 changes: 6 additions & 7 deletions core/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,25 +299,24 @@ func getConnections() *C.char {
}

//export closeConnections
func closeConnections() bool {
func closeConnections() {
statistic.DefaultManager.Range(func(c statistic.Tracker) bool {
err := c.Close()
if err != nil {
return false
}
return true
})
return true
}

//export closeConnection
func closeConnection(id *C.char) bool {
func closeConnection(id *C.char) {
connectionId := C.GoString(id)
err := statistic.DefaultManager.Get(connectionId).Close()
if err != nil {
return false
c := statistic.DefaultManager.Get(connectionId)
if c == nil {
return
}
return true
_ = c.Close()
}

//export getProviders
Expand Down
4 changes: 1 addition & 3 deletions lib/application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ class ApplicationState extends State<Application> {
GlobalWidgetsLocalizations.delegate
],
builder: (_, child) {
return PopContainer(
child: _buildApp(child!),
);
return _buildApp(child!);
},
scrollBehavior: BaseScrollBehavior(),
title: appName,
Expand Down
6 changes: 5 additions & 1 deletion lib/clash/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,15 @@ class ClashCore {
return connectionsRaw.map((e) => Connection.fromJson(e)).toList();
}

closeConnections(String id) {
closeConnection(String id) {
final idChar = id.toNativeUtf8().cast<Char>();
clashFFI.closeConnection(idChar);
malloc.free(idChar);
}

closeConnections() {
clashFFI.closeConnections();
}
}

final clashCore = ClashCore();
12 changes: 6 additions & 6 deletions lib/clash/generated/clash_ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5351,16 +5351,16 @@ class ClashFFI {
late final _getConnections =
_getConnectionsPtr.asFunction<ffi.Pointer<ffi.Char> Function()>();

int closeConnections() {
void closeConnections() {
return _closeConnections();
}

late final _closeConnectionsPtr =
_lookup<ffi.NativeFunction<GoUint8 Function()>>('closeConnections');
_lookup<ffi.NativeFunction<ffi.Void Function()>>('closeConnections');
late final _closeConnections =
_closeConnectionsPtr.asFunction<int Function()>();
_closeConnectionsPtr.asFunction<void Function()>();

int closeConnection(
void closeConnection(
ffi.Pointer<ffi.Char> id,
) {
return _closeConnection(
Expand All @@ -5369,10 +5369,10 @@ class ClashFFI {
}

late final _closeConnectionPtr =
_lookup<ffi.NativeFunction<GoUint8 Function(ffi.Pointer<ffi.Char>)>>(
_lookup<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Char>)>>(
'closeConnection');
late final _closeConnection =
_closeConnectionPtr.asFunction<int Function(ffi.Pointer<ffi.Char>)>();
_closeConnectionPtr.asFunction<void Function(ffi.Pointer<ffi.Char>)>();

ffi.Pointer<ffi.Char> getProviders() {
return _getProviders();
Expand Down
54 changes: 54 additions & 0 deletions lib/common/iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,58 @@ extension IterableExt<T> on Iterable<T> {
yield iterator.current;
}
}

Iterable<List<T>> chunks(int size) sync* {
if (length == 0) return;
var iterator = this.iterator;
while (iterator.moveNext()) {
var chunk = [iterator.current];
for (var i = 1; i < size && iterator.moveNext(); i++) {
chunk.add(iterator.current);
}
yield chunk;
}
}

Iterable<T> fill(
int length, {
required T Function(int count) filler,
}) sync* {
int count = 0;
for (var item in this) {
yield item;
count++;
if (count >= length) return;
}
while (count < length) {
yield filler(count);
count++;
}
}
}

extension DoubleListExt on List<double> {
int findInterval(num target) {
if (isEmpty) return -1;
if (target < first) return -1;
if (target >= last) return length - 1;

int left = 0;
int right = length - 1;

while (left <= right) {
int mid = left + (right - left) ~/ 2;

if (mid == length - 1 ||
(this[mid] <= target && target < this[mid + 1])) {
return mid;
} else if (target < this[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
}

return -1; // 这行理论上不会执行到,但为了完整性保留
}
}
11 changes: 11 additions & 0 deletions lib/common/scroll.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@ class BaseScrollBehavior extends MaterialScrollBehavior {
PointerDeviceKind.unknown,
};
}

class HiddenBarScrollBehavior extends BaseScrollBehavior {
@override
Widget buildScrollbar(
BuildContext context,
Widget child,
ScrollableDetails details,
) {
return child;
}
}
2 changes: 1 addition & 1 deletion lib/common/window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Window {
await windowManager.ensureInitialized();
WindowOptions windowOptions = WindowOptions(
size: Size(props.width, props.height),
minimumSize: const Size(380, 600),
minimumSize: const Size(380, 500),
titleBarStyle: TitleBarStyle.hidden,
);
if (props.left != null || props.top != null) {
Expand Down
11 changes: 8 additions & 3 deletions lib/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class AppController {
final updateId = config.profiles.first.id;
changeProfile(updateId);
} else {
changeProfile(null);
updateSystemProxy(false);
}
}
}
Expand Down Expand Up @@ -193,6 +193,7 @@ class AppController {
}

handleBackOrExit() async {
print(config.isMinimizeOnExit);
if (config.isMinimizeOnExit) {
if (system.isDesktop) {
await savePreferences();
Expand Down Expand Up @@ -410,8 +411,7 @@ class AppController {
addProfileFormURL(url);
}

int get columns =>
other.getColumns(appState.viewMode, config.proxiesColumns);
int get columns => other.getColumns(appState.viewMode, config.proxiesColumns);

updateViewWidth(double width) {
WidgetsBinding.instance.addPostFrameCallback((_) {
Expand Down Expand Up @@ -453,4 +453,9 @@ class AppController {
ProxiesSortType.name => _sortOfName(proxies),
};
}

String getCurrentSelectedName(String groupName) {
final group = appState.getGroupWithName(groupName);
return config.currentSelectedMap[groupName] ?? group?.now ?? '';
}
}
3 changes: 1 addition & 2 deletions lib/fragments/about.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:fl_clash/common/common.dart';
import 'package:fl_clash/state.dart';
import 'package:fl_clash/widgets/list.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

@immutable
class Contributor {
Expand Down Expand Up @@ -90,7 +89,7 @@ class AboutFragment extends StatelessWidget {
];
return generateSection(
separated: false,
title: appLocalizations.contributors,
title: appLocalizations.otherContributors,
items: [
ListItem(
title: SingleChildScrollView(
Expand Down
21 changes: 19 additions & 2 deletions lib/fragments/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class _ConfigFragmentState extends State<ConfigFragment> {
return generateSection(
title: appLocalizations.app,
items: [
if (Platform.isAndroid)
if (Platform.isAndroid)...[
Selector<Config, bool>(
selector: (_, config) => config.allowBypass,
builder: (_, allowBypass, __) {
Expand All @@ -159,7 +159,6 @@ class _ConfigFragmentState extends State<ConfigFragment> {
);
},
),
if (Platform.isAndroid)
Selector<Config, bool>(
selector: (_, config) => config.systemProxy,
builder: (_, systemProxy, __) {
Expand All @@ -177,6 +176,24 @@ class _ConfigFragmentState extends State<ConfigFragment> {
);
},
),
],
Selector<Config, bool>(
selector: (_, config) => config.isCloseConnections,
builder: (_, isCloseConnections, __) {
return ListItem.switchItem(
leading: const Icon(Icons.auto_delete_outlined),
title: Text(appLocalizations.autoCloseConnections),
subtitle: Text(appLocalizations.autoCloseConnectionsDesc),
delegate: SwitchDelegate(
value: isCloseConnections,
onChanged: (bool value) async {
final appController = globalState.appController;
appController.config.isCloseConnections = value;
},
),
);
},
),
Selector<Config, bool>(
selector: (_, config) => config.isCompatible,
builder: (_, isCompatible, __) {
Expand Down
21 changes: 17 additions & 4 deletions lib/fragments/connections.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ class _ConnectionsFragmentState extends State<ConnectionsFragment> {
timer = Timer.periodic(
const Duration(seconds: 1),
(timer) {
connectionsNotifier.value = connectionsNotifier.value
.copyWith(connections: clashCore.getConnections());
connectionsNotifier.value = connectionsNotifier.value.copyWith(
connections: clashCore.getConnections(),
);
},
);
});
Expand All @@ -50,6 +51,18 @@ class _ConnectionsFragmentState extends State<ConnectionsFragment> {
final commonScaffoldState =
context.findAncestorStateOfType<CommonScaffoldState>();
commonScaffoldState?.actions = [
IconButton(
onPressed: () {
clashCore.closeConnections();
connectionsNotifier.value = connectionsNotifier.value.copyWith(
connections: clashCore.getConnections(),
);
},
icon: const Icon(Icons.delete_sweep_outlined),
),
const SizedBox(
width: 8,
),
IconButton(
onPressed: () {
showSearch(
Expand Down Expand Up @@ -87,7 +100,7 @@ class _ConnectionsFragmentState extends State<ConnectionsFragment> {
}

_handleBlockConnection(String id) {
clashCore.closeConnections(id);
clashCore.closeConnection(id);
connectionsNotifier.value = connectionsNotifier.value
.copyWith(connections: clashCore.getConnections());
}
Expand Down Expand Up @@ -227,7 +240,7 @@ class ConnectionsSearchDelegate extends SearchDelegate {
}

_handleBlockConnection(String id) {
clashCore.closeConnections(id);
clashCore.closeConnection(id);
connectionsNotifier.value = connectionsNotifier.value.copyWith(
connections: clashCore.getConnections(),
);
Expand Down
2 changes: 1 addition & 1 deletion lib/fragments/dashboard/dashboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class _DashboardFragmentState extends State<DashboardFragment> {
// final viewMode = other.getViewMode(viewWidth);
// final isDesktop = viewMode == ViewMode.desktop;
return Grid(
crossAxisCount: max(4 * ((viewWidth / 320).ceil()), 8),
crossAxisCount: max(4 * ((viewWidth / 350).ceil()), 8),
crossAxisSpacing: 16,
mainAxisSpacing: 16,
children: const [
Expand Down
7 changes: 0 additions & 7 deletions lib/fragments/profiles/profiles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,6 @@ class _ProfileItemState extends State<ProfileItem> {
),
Row(
children: [
Text(
appLocalizations.expirationTime,
style: textTheme.labelMedium?.toLighter,
),
const SizedBox(
width: 4,
),
Text(
expireShow,
style: textTheme.labelMedium?.toLighter,
Expand Down
9 changes: 4 additions & 5 deletions lib/fragments/proxies/card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,10 @@ class ProxyCard extends StatelessWidget {
groupName,
proxy.name,
);
clashCore.changeProxy(
ChangeProxyParams(
groupName: groupName,
proxyName: proxy.name,
),
globalState.changeProxy(
config: appController.config,
groupName: groupName,
proxyName: proxy.name,
);
}

Expand Down
Loading

0 comments on commit 6de89d7

Please sign in to comment.