Skip to content

Commit

Permalink
✨ add File Explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
H2Sxxa committed Jul 29, 2024
1 parent 486718e commit 1f8817c
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 7 deletions.
11 changes: 5 additions & 6 deletions lib/views/pages/manage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ class _DistributionManagePageState extends State<DistributionManagePage> {
child: Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
leading: BackButton(
onPressed: () => setState(() {
current = null;
}),
),
forceMaterialTransparency: true,
backgroundColor: Colors.transparent,
title: Text(current!),
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.arrow_back),
onPressed: () => setState(() {
current = null;
}),
),
body: DistroManagePage(distro: current!),
),
);
Expand Down
108 changes: 108 additions & 0 deletions lib/views/pages/manage_distro.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'package:arche/arche.dart';
import 'package:arche/extensions/iter.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:path/path.dart';
import 'package:wslconfigurer/i18n/i18n.dart';
import 'package:wslconfigurer/views/widgets/basic.dart';
import 'package:wslconfigurer/views/widgets/extension.dart';
Expand Down Expand Up @@ -119,6 +121,12 @@ class _DistroManagePageState extends State<DistroManagePage>
),
),
),
Card.filled(
child: Padding(
padding: const EdgeInsets.all(8),
child: WSLExplorerWidget(distro: widget.distro),
),
),
].enumerate(
(index, widget) => AnimationConfiguration.staggeredList(
position: index,
Expand All @@ -134,3 +142,103 @@ class _DistroManagePageState extends State<DistroManagePage>
);
}
}

class WSLExplorerWidget extends StatefulWidget {
final String distro;
const WSLExplorerWidget({super.key, required this.distro});

@override
State<StatefulWidget> createState() => _WSLExplorerWidgetState();
}

class _WSLExplorerWidgetState extends State<WSLExplorerWidget>
with RefreshMountedStateMixin {
late WSLExplorer explorer;
List<FileSystemEntity> current = [];
@override
void initState() {
super.initState();

explorer = WSLExplorer(widget.distro);
listExplorer();
}

void listExplorer() {
current.clear();

explorer.list().listen((data) => refreshMountedFn(() => current.add(data)),
onDone: () => refreshMountedFn(
() => current.sort((a, b) => a.path.compareTo(b.path))));
}

@override
Widget build(BuildContext context) {
return Column(
key: ValueKey(explorer.current),
children: [
...explorer.isRoot
? []
: [
ListTile(
title: const Text(".."),
onTap: () {
explorer.parent();
listExplorer();
},
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(8))))
],
...current.map<Widget>((entity) {
var entityBaseName = basename(entity.path);
var friendlyName =
entity.path.replaceFirst(explorer.root, "").replaceAll("\\", "/");
return FutureBuilder(
future: entity.stat(),
builder: (context, snapshot) {
var data = snapshot.data;
if (data == null) {
return ListTile(
leading: const Icon(Icons.question_mark),
title: Text(entityBaseName),
subtitle: Text(friendlyName),
);
}

IconData icon;
Function()? onTap;

switch (data.type) {
case FileSystemEntityType.file:
icon = FontAwesomeIcons.file;
break;
case FileSystemEntityType.directory:
icon = Icons.folder;
onTap = () async {
await explorer.move(entity.path);
listExplorer();
};
break;
case FileSystemEntityType.link:
icon = Icons.link;
break;
case FileSystemEntityType.notFound:
icon = Icons.security;
default:
icon = Icons.question_mark;
}

return ListTile(
leading: Icon(icon),
title: Text(entityBaseName),
subtitle: Text(friendlyName),
onTap: onTap,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(8))),
);
},
);
}),
],
);
}
}
30 changes: 30 additions & 0 deletions lib/windows/wsl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,33 @@ class WindowsSubSystemLinux {
);
}
}

class WSLExplorer {
final String distro;
late final String root;
late Directory current;

WSLExplorer(this.distro) {
root = "//wsl.localhost/$distro";
current = Directory("$root/home");
}

Stream<FileSystemEntity> list() {
return current.list();
}

bool get isRoot => current.path == root;

Future<bool> move(String path) async {
var target = Directory(path);
if (await target.exists()) {
current = target;
return true;
}
return false;
}

void parent() {
current = current.parent;
}
}
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ packages:
source: hosted
version: "2.1.0"
path:
dependency: transitive
dependency: "direct main"
description:
name: path
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies:
flutter_markdown: ^0.7.3
flutter_staggered_animations: ^1.1.1
system_info2: ^4.0.0
path: ^1.9.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 1f8817c

Please sign in to comment.