From e39fb23b66f07fdb2890c87dfb148b3fd9419da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?lollipopkit=F0=9F=8F=B3=EF=B8=8F=E2=80=8D=E2=9A=A7?= =?UTF-8?q?=EF=B8=8F?= <10864310+lollipopkit@users.noreply.github.com> Date: Tue, 14 Jan 2025 11:19:47 +0800 Subject: [PATCH] bug: unix perm switcher (#674) --- lib/view/widget/unix_perm.dart | 62 +++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/lib/view/widget/unix_perm.dart b/lib/view/widget/unix_perm.dart index ad8d053e..ecddd2e4 100644 --- a/lib/view/widget/unix_perm.dart +++ b/lib/view/widget/unix_perm.dart @@ -21,6 +21,21 @@ final class RWX { } } +enum UnixPermScope { + user, + group, + other, + ; + + String get title { + return switch (this) { + user => 'User', + group => 'Group', + other => 'Other', + }; + } +} + final class UnixPerm { final RWX user; final RWX group; @@ -40,6 +55,17 @@ final class UnixPerm { ); } + UnixPerm copyWithScope(UnixPermScope scope, RWX rwx) { + switch (scope) { + case UnixPermScope.user: + return copyWith(user: rwx); + case UnixPermScope.group: + return copyWith(group: rwx); + case UnixPermScope.other: + return copyWith(other: rwx); + } + } + /// eg.: 744 String get perm { return '${user.value}${group.value}${other.value}'; @@ -55,8 +81,12 @@ final class UnixPerm { final class UnixPermEditor extends StatefulWidget { final UnixPerm perm; final void Function(UnixPerm) onChanged; - const UnixPermEditor( - {super.key, required this.perm, required this.onChanged}); + + const UnixPermEditor({ + super.key, + required this.perm, + required this.onChanged, + }); @override State createState() => _UnixPermEditorState(); @@ -79,41 +109,41 @@ final class _UnixPermEditorState extends State { const Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ - Text('R'), - Text('W'), - Text('X'), + Text('Read'), + Text('Write'), + Text('Exec'), ], ).paddingOnly(left: 13), UIs.height7, - _buildRow('U', perm.user), - _buildRow('G', perm.group), - _buildRow('O', perm.other), + _buildRow(UnixPermScope.user, perm.user), + _buildRow(UnixPermScope.group, perm.group), + _buildRow(UnixPermScope.other, perm.other), ], ); } - Widget _buildRow(String title, RWX rwx) { + Widget _buildRow(UnixPermScope scope, RWX rwx) { return Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - SizedBox(width: 7, child: Text(title)), + SizedBox(width: 7, child: Text(scope.title)), _buildSwitch(rwx.r, (v) { setState(() { - perm = perm.copyWith(user: rwx.copyWith(r: v)); + perm = perm.copyWithScope(scope, rwx.copyWith(r: v)); + widget.onChanged(perm); }); - widget.onChanged(perm); }), _buildSwitch(rwx.w, (v) { setState(() { - perm = perm.copyWith(user: rwx.copyWith(w: v)); + perm = perm.copyWithScope(scope, rwx.copyWith(w: v)); + widget.onChanged(perm); }); - widget.onChanged(perm); }), _buildSwitch(rwx.x, (v) { setState(() { - perm = perm.copyWith(user: rwx.copyWith(x: v)); + perm = perm.copyWithScope(scope, rwx.copyWith(x: v)); + widget.onChanged(perm); }); - widget.onChanged(perm); }), ], );