autofocus setting

This commit is contained in:
randogoth 2025-02-24 14:04:31 +02:00
parent f87e554df0
commit f0cf7b6af2

View file

@ -17,6 +17,7 @@ class AppSettings {
bool colorIcons; // true = color icons; false = monochrome icons
bool darkTheme;
bool sortAlphabetically;
bool autoFocus; // new
AppSettings({
this.showTags = true,
@ -24,6 +25,7 @@ class AppSettings {
this.colorIcons = true,
this.darkTheme = false,
this.sortAlphabetically = false,
this.autoFocus = true,
});
}
@ -43,14 +45,13 @@ class _MyAppState extends State<MyApp> {
Future<void> _loadSettings() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool? savedSorting = prefs.getBool('sortAlphabetically');
print('Saved sorting: $savedSorting'); // Debug print
setState(() {
settings.showTags = prefs.getBool('showTags') ?? true;
settings.showIcons = prefs.getBool('showIcons') ?? true;
settings.colorIcons = prefs.getBool('colorIcons') ?? true;
settings.darkTheme = prefs.getBool('darkTheme') ?? false;
settings.sortAlphabetically = prefs.getBool('sortAlphabetically') ?? false;
settings.autoFocus = prefs.getBool('autoFocus') ?? true;
});
}
@ -68,6 +69,7 @@ class _MyAppState extends State<MyApp> {
_updateSetting('colorIcons', settings.colorIcons);
_updateSetting('darkTheme', settings.darkTheme);
_updateSetting('sortAlphabetically', settings.sortAlphabetically);
_updateSetting('autoFocus', settings.autoFocus);
}
@override
@ -123,6 +125,7 @@ class AppLauncherScreen extends StatefulWidget {
class _AppLauncherScreenState extends State<AppLauncherScreen> {
final TextEditingController searchController = TextEditingController();
final FocusNode searchFocusNode = FocusNode();
List<AppInfo> apps = [];
List<AppInfo> filteredApps = [];
bool isLoading = true;
@ -132,6 +135,11 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
super.initState();
_loadInstalledApps();
searchController.addListener(_filterApps);
WidgetsBinding.instance.addPostFrameCallback((_) {
if (widget.settings.autoFocus) {
FocusScope.of(context).requestFocus(searchFocusNode);
}
});
}
Future<void> _loadInstalledApps() async {
@ -143,11 +151,27 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
List<AppInfo> loadedApps = installedApps.map((app) {
final lastUsedMillis = prefs.getInt("lastUsed_${app.packageName}");
final storedTags = prefs.getString("tags_${app.packageName}");
List<String> tags = [];
if (storedTags != null && storedTags.trim().isNotEmpty) {
// Use the stored user-edited tags.
tags = storedTags.split(" ");
} else {
// Fall back to using the auto-generated category tag.
if (app.category != null) {
String categoryStr = app.category.toString().split('.').last;
if (categoryStr.toLowerCase() != 'undefined') {
tags.add('@' + categoryStr.toLowerCase());
}
}
}
return AppInfo(
icon: app is ApplicationWithIcon ? app.icon : null,
name: app.appName,
packageName: app.packageName,
tags: [],
tags: tags,
lastUsed: lastUsedMillis != null
? DateTime.fromMillisecondsSinceEpoch(lastUsedMillis)
: null,
@ -156,9 +180,9 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
setState(() {
apps = loadedApps;
filteredApps = apps;
isLoading = false;
});
// Force initial sorting (by last used if sortAlphabetically is false)
_filterApps();
}
@ -244,13 +268,15 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
),
actions: [
TextButton(
onPressed: () {
onPressed: () async {
setState(() {
app.tags
..clear()
..addAll(
tagController.text.split(" ").where((t) => t.isNotEmpty));
..addAll(tagController.text.split(" ").where((t) => t.isNotEmpty));
});
// Persist the updated tags.
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString("tags_${app.packageName}", app.tags.join(" "));
Navigator.of(context).pop();
_filterApps();
},
@ -265,6 +291,37 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
);
}
List<InlineSpan> _formatTags(String content) {
final tagPattern = RegExp(r'([#@+][a-zA-Z0-9_]+)');
final Set<String> tagSet = {}; // Avoid duplicate tags
for (final match in tagPattern.allMatches(content)) {
tagSet.add(match.group(0)!);
}
// Sort tags: +tags before @tags before #tags
final sortedTags = tagSet.toList()
..sort((a, b) {
if (a.startsWith('+') && !b.startsWith('+')) return -1;
if (b.startsWith('+') && !a.startsWith('+')) return 1;
if (a.startsWith('@') && !b.startsWith('@')) return -1;
if (b.startsWith('@') && !a.startsWith('@')) return 1;
return a.compareTo(b); // Default alphabetical order
});
return sortedTags.map((tag) {
Color tagColor = Colors.white;
if (tag.startsWith('#')) tagColor = Colors.pinkAccent;
if (tag.startsWith('+')) tagColor = Colors.cyan;
if (tag.startsWith('@')) tagColor = Colors.orange;
return TextSpan(
text: "$tag ",
style: TextStyle(color: tagColor, fontWeight: FontWeight.bold),
);
}).toList();
}
Widget _buildAppIcon(AppInfo app) {
Widget iconWidget;
if (app.icon != null) {
@ -298,6 +355,7 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
return Scaffold(
appBar: AppBar(
title: TextField(
focusNode: widget.settings.autoFocus ? searchFocusNode : null,
controller: searchController,
decoration: InputDecoration(
hintText: 'Search apps...',
@ -308,6 +366,12 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
actions: [
Padding(
padding: EdgeInsets.symmetric(horizontal: 15),
child: GestureDetector(
onTap: () {
if (searchController.text.isNotEmpty) {
searchController.clear();
}
},
child: PopupMenuButton<String>(
enabled: searchController.text.isEmpty,
icon: SvgPicture.asset(
@ -324,6 +388,7 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
colorIcons: widget.settings.colorIcons,
darkTheme: widget.settings.darkTheme,
sortAlphabetically: widget.settings.sortAlphabetically,
autoFocus: widget.settings.autoFocus,
));
break;
case 'Toggle Icons':
@ -333,6 +398,7 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
colorIcons: widget.settings.colorIcons,
darkTheme: widget.settings.darkTheme,
sortAlphabetically: widget.settings.sortAlphabetically,
autoFocus: widget.settings.autoFocus,
));
break;
case 'Toggle Color Icons':
@ -342,6 +408,7 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
colorIcons: !widget.settings.colorIcons,
darkTheme: widget.settings.darkTheme,
sortAlphabetically: widget.settings.sortAlphabetically,
autoFocus: widget.settings.autoFocus,
));
break;
case 'Toggle Theme':
@ -351,6 +418,7 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
colorIcons: widget.settings.colorIcons,
darkTheme: !widget.settings.darkTheme,
sortAlphabetically: widget.settings.sortAlphabetically,
autoFocus: widget.settings.autoFocus,
));
break;
case 'Toggle Sorting':
@ -360,9 +428,20 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
colorIcons: widget.settings.colorIcons,
darkTheme: widget.settings.darkTheme,
sortAlphabetically: !widget.settings.sortAlphabetically,
autoFocus: widget.settings.autoFocus,
));
_filterApps();
break;
case 'Toggle AutoFocus':
widget.onSettingsChanged(AppSettings(
showTags: widget.settings.showTags,
showIcons: widget.settings.showIcons,
colorIcons: widget.settings.colorIcons,
darkTheme: widget.settings.darkTheme,
sortAlphabetically: widget.settings.sortAlphabetically,
autoFocus: !widget.settings.autoFocus,
));
break;
}
},
itemBuilder: (BuildContext context) => [
@ -419,9 +498,17 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
: 'Sort by Last Used'),
),
),
PopupMenuItem<String>(
value: 'Toggle AutoFocus',
child: ListTile(
leading: Icon(Icons.center_focus_strong),
title: Text(widget.settings.autoFocus ? 'Disable Auto Focus' : 'Enable Auto Focus'),
),
),
],
),
),
),
],
),
body: isLoading
@ -450,7 +537,15 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
widget.settings.showIcons ? _buildAppIcon(app) : null,
title: Text(app.name),
subtitle:
widget.settings.showTags ? Text(app.tags.join(" ")) : null,
widget.settings.showTags
? RichText(
text: TextSpan(
style: DefaultTextStyle.of(context).style,
children: _formatTags(app.tags.join(' ')), // Use stored tags
),
overflow: TextOverflow.ellipsis,
)
: null,
onTap: () => _launchApp(app),
),
);