bug: unix perm switcher (#674)

This commit is contained in:
lollipopkit🏳️‍⚧️
2025-01-14 11:19:47 +08:00
committed by GitHub
parent 4777166dd9
commit e39fb23b66

View File

@@ -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 class UnixPerm {
final RWX user; final RWX user;
final RWX group; 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 /// eg.: 744
String get perm { String get perm {
return '${user.value}${group.value}${other.value}'; return '${user.value}${group.value}${other.value}';
@@ -55,8 +81,12 @@ final class UnixPerm {
final class UnixPermEditor extends StatefulWidget { final class UnixPermEditor extends StatefulWidget {
final UnixPerm perm; final UnixPerm perm;
final void Function(UnixPerm) onChanged; 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 @override
State<UnixPermEditor> createState() => _UnixPermEditorState(); State<UnixPermEditor> createState() => _UnixPermEditorState();
@@ -79,41 +109,41 @@ final class _UnixPermEditorState extends State<UnixPermEditor> {
const Row( const Row(
mainAxisAlignment: MainAxisAlignment.spaceAround, mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [ children: [
Text('R'), Text('Read'),
Text('W'), Text('Write'),
Text('X'), Text('Exec'),
], ],
).paddingOnly(left: 13), ).paddingOnly(left: 13),
UIs.height7, UIs.height7,
_buildRow('U', perm.user), _buildRow(UnixPermScope.user, perm.user),
_buildRow('G', perm.group), _buildRow(UnixPermScope.group, perm.group),
_buildRow('O', perm.other), _buildRow(UnixPermScope.other, perm.other),
], ],
); );
} }
Widget _buildRow(String title, RWX rwx) { Widget _buildRow(UnixPermScope scope, RWX rwx) {
return Row( return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [ children: [
SizedBox(width: 7, child: Text(title)), SizedBox(width: 7, child: Text(scope.title)),
_buildSwitch(rwx.r, (v) { _buildSwitch(rwx.r, (v) {
setState(() { 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) { _buildSwitch(rwx.w, (v) {
setState(() { 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) { _buildSwitch(rwx.x, (v) {
setState(() { setState(() {
perm = perm.copyWith(user: rwx.copyWith(x: v)); perm = perm.copyWithScope(scope, rwx.copyWith(x: v));
widget.onChanged(perm);
}); });
widget.onChanged(perm);
}), }),
], ],
); );