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 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<UnixPermEditor> createState() => _UnixPermEditorState();
@@ -79,41 +109,41 @@ final class _UnixPermEditorState extends State<UnixPermEditor> {
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);
});
}),
_buildSwitch(rwx.w, (v) {
setState(() {
perm = perm.copyWith(user: rwx.copyWith(w: v));
});
perm = perm.copyWithScope(scope, rwx.copyWith(w: v));
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);
});
}),
],
);