Fix icon loading
This fixes the loading of OS icons so they are correctly replaced with a generic computer if no matching icon is found. It also centralises the loading process so it happens once during app initialisation, and displays the corresponding icon next to each VM on the manager screen.
This commit is contained in:
parent
d3fe2c8c4f
commit
208d4f8dc9
|
@ -1,10 +1,12 @@
|
|||
import 'dart:io';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:tuple/tuple.dart';
|
||||
import 'package:window_size/window_size.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import 'src/app.dart';
|
||||
import 'src/mixins/app_version.dart';
|
||||
|
@ -12,6 +14,7 @@ import 'src/model/app_settings.dart';
|
|||
import 'src/model/operating_system.dart';
|
||||
import 'src/model/option.dart';
|
||||
import 'src/model/version.dart';
|
||||
import 'src/model/osicons.dart';
|
||||
|
||||
Future<List<OperatingSystem>> loadOperatingSystems(bool showUbuntus) async {
|
||||
var process = await Process.run('quickget', ['list_csv']);
|
||||
|
@ -54,6 +57,22 @@ Future<List<OperatingSystem>> loadOperatingSystems(bool showUbuntus) async {
|
|||
return output;
|
||||
}
|
||||
|
||||
Future<void> getIcons() async {
|
||||
final manifestContent = await rootBundle.loadString('AssetManifest.json');
|
||||
final Map<String, dynamic> manifestMap = json.decode(manifestContent);
|
||||
final imagePaths = manifestMap.keys
|
||||
.where((String key) => key.contains('quickemu-icons/'))
|
||||
.where((String key) => key.contains('.svg'))
|
||||
.toList();
|
||||
for (final imagePath in imagePaths) {
|
||||
String filename = imagePath
|
||||
.split('/')
|
||||
.last;
|
||||
String id = filename.substring(0, filename.lastIndexOf('.'));
|
||||
osIcons[id] = imagePath;
|
||||
}
|
||||
}
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
// Don't forget to also change the size in linux/my_application.cc:50
|
||||
|
@ -62,6 +81,7 @@ void main() async {
|
|||
final foundQuickGet = await Process.run('which', ['quickget']);
|
||||
if (foundQuickGet.exitCode == 0) {
|
||||
gOperatingSystems = await loadOperatingSystems(false);
|
||||
getIcons();
|
||||
AppVersion.packageInfo = await PackageInfo.fromPlatform();
|
||||
}
|
||||
runApp(
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
// Store a list of OS icons against the OS name.
|
||||
|
||||
Map<String, String> osIcons = {};
|
|
@ -4,7 +4,6 @@ import 'dart:core';
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_svg/flutter_svg.dart';
|
||||
import 'package:gettext_i18n/gettext_i18n.dart';
|
||||
|
@ -14,6 +13,7 @@ import 'package:process_run/shell.dart';
|
|||
import '../globals.dart';
|
||||
import '../mixins/preferences_mixin.dart';
|
||||
import '../model/vminfo.dart';
|
||||
import '../model/osicons.dart';
|
||||
|
||||
/// VM manager page.
|
||||
/// Displays a list of available VMs, running state and connection info,
|
||||
|
@ -255,8 +255,22 @@ class _ManagerState extends State<Manager> with PreferencesMixin {
|
|||
});
|
||||
}
|
||||
}
|
||||
String vmStem = currentVm;
|
||||
SvgPicture? osIcon;
|
||||
while(vmStem.contains('-')) {
|
||||
vmStem = vmStem.substring(0, vmStem.lastIndexOf('-'));
|
||||
if (osIcons.containsKey(vmStem)) {
|
||||
osIcon = SvgPicture.asset(
|
||||
osIcons[vmStem]!,
|
||||
width: 32,
|
||||
height: 32,
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return <Widget>[
|
||||
ListTile(
|
||||
leading: osIcon ?? const Icon(Icons.computer, size: 32),
|
||||
title: Text(currentVm),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
|
|
|
@ -3,6 +3,7 @@ import 'package:flutter_svg/svg.dart';
|
|||
import 'package:gettext_i18n/gettext_i18n.dart';
|
||||
|
||||
import '../model/operating_system.dart';
|
||||
import '../model/osicons.dart';
|
||||
|
||||
class OperatingSystemSelection extends StatefulWidget {
|
||||
const OperatingSystemSelection({Key? key}) : super(key: key);
|
||||
|
@ -76,13 +77,13 @@ class _OperatingSystemSelectionState extends State<OperatingSystemSelection> {
|
|||
var item = list[index];
|
||||
Widget icon;
|
||||
|
||||
try {
|
||||
if (osIcons.containsKey(item.code)) {
|
||||
icon = SvgPicture.asset(
|
||||
"assets/quickemu-icons/${item.code}.svg",
|
||||
osIcons[item.code]!,
|
||||
width: 32,
|
||||
height: 32,
|
||||
);
|
||||
} catch (e) {
|
||||
} else {
|
||||
// Replace with generic icon
|
||||
icon = const Icon(Icons.computer, size: 32);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue