can now set launch options in the settings directly

This commit is contained in:
ALEZ-DEV 2024-08-27 18:10:07 +02:00
parent 2bbcc3e5af
commit 530a30eac7
9 changed files with 174 additions and 10 deletions

View File

@ -30,7 +30,7 @@ async fn main() {
debug!("Launch option -> {:?}", args.options);
if let Some(command) = args.set_options {
GameConfig::set_launch_options(command)
GameConfig::set_launch_options(Some(command))
.await
.expect("Failed to save launch options into the config file");
}

View File

@ -71,9 +71,9 @@ impl GameConfig {
}
}
pub async fn set_launch_options(command: String) -> anyhow::Result<()> {
pub async fn set_launch_options(command: Option<String>) -> anyhow::Result<()> {
let mut config = Self::get_config().await;
config.launch_options = Some(command);
config.launch_options = command;
Self::save_config(config).await?;
Ok(())
}

View File

@ -23,4 +23,24 @@ class Config {
_isLoadingConfig = false;
}
}
static bool _isLoadingGetLaunchOptions = false;
static Future<String?> getLaunchOptions() async {
if (!_isLoadingGetLaunchOptions) {
_isLoadingGetLaunchOptions = true;
GetLaunchOptionsInput().sendSignalToRust();
final stream = GetLaunchOptionsOutput.rustSignalStream;
await for (final rustSignal in stream) {
_isLoadingGetLaunchOptions = false;
return rustSignal.message.launchOptions;
}
}
return null;
}
static void setLaunchOptions(String? newLaunchOptions) {
SetLaunchOptionsInput(launchOptions: newLaunchOptions).sendSignalToRust();
}
}

View File

@ -3,6 +3,8 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import './config.dart';
enum BackgroundType {
contain,
fill,
@ -14,12 +16,18 @@ enum BackgroundType {
class Settings {
final SharedPreferences prefs;
String? _launchOptions;
Settings({required this.prefs});
Settings({required this.prefs, required String? launchOptions})
: _launchOptions = launchOptions;
static Future<Settings> create() async {
final prefs = await SharedPreferences.getInstance();
return Settings(prefs: prefs);
final launchOptions = await Config.getLaunchOptions();
return Settings(
prefs: prefs,
launchOptions: launchOptions,
);
}
String firstTimeKey = 'first_time';
@ -31,6 +39,15 @@ class Settings {
prefs.setBool(firstTimeKey, value!);
}
String? get launchOptions {
return _launchOptions;
}
set launchOptions(String? value) {
Config.setLaunchOptions(value);
_launchOptions = value;
}
BackgroundType? _backgroundType;
String backgroundTypeKey = 'background_type';

View File

@ -45,4 +45,12 @@ class SettingsProvider with ChangeNotifier {
set backgroundId(int? value) {
_settings.backgroundId = value;
}
String? get launchOptions {
return _settings.launchOptions;
}
set launchOptions(String? value) {
_settings.launchOptions = value;
}
}

View File

@ -1,12 +1,78 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:yaru/yaru.dart';
class GeneralSettingsPage extends StatelessWidget {
import './../../providers/settings_provider.dart';
class GeneralSettingsPage extends StatefulWidget {
const GeneralSettingsPage({super.key});
@override
State<GeneralSettingsPage> createState() => _GeneralSettingsPageState();
}
class _GeneralSettingsPageState extends State<GeneralSettingsPage> {
final textFieldController = TextEditingController();
String? _launchOptionsError;
bool isLaunchOptionsValid(String launchOptions) {
return RegExp(r'^.*%command%.*$').hasMatch(launchOptions);
}
@override
Widget build(BuildContext context) {
return const Center(
child: Text("General settings"),
final provider = Provider.of<SettingsProvider>(context, listen: false);
final launchOptions = provider.launchOptions;
return Center(
child: Column(
children: [
const Center(
child: Text(
'General',
style: TextStyle(
fontSize: 36,
),
),
),
Padding(
padding: const EdgeInsets.all(15.0),
child: SizedBox(
child: YaruSection(
headline: const Text('Launch options'),
child: TextFormField(
initialValue: launchOptions,
//controller: textFieldController,
decoration: InputDecoration(
labelText:
'Your custom launch options, E.G. : mangohud %command%',
errorText: _launchOptionsError,
),
validator: (value) => _launchOptionsError,
onChanged: (String text) {
if (text.isEmpty) {
setState(() {
_launchOptionsError = null;
});
provider.launchOptions = null;
} else if (!isLaunchOptionsValid(text)) {
setState(() {
_launchOptionsError =
'You need to put \'%command%\' in your command';
});
} else {
setState(() {
_launchOptionsError = null;
});
provider.launchOptions = text;
}
},
),
),
),
),
],
),
);
}
}

View File

@ -8,3 +8,16 @@ message ConfigInput {}
message ConfigOutput {
string configPath = 1;
}
// [RINF:DART-SIGNAL]
message GetLaunchOptionsInput {}
// [RINF:RUST-SIGNAL]
message GetLaunchOptionsOutput {
optional string launchOptions = 1;
}
// [RINF:DART-SIGNAL]
message SetLaunchOptionsInput {
optional string launchOptions = 1;
}

View File

@ -1,6 +1,12 @@
use babylonia_terminal_sdk::game_config::GameConfig;
use crate::messages::config::{ConfigInput, ConfigOutput};
use crate::messages::{
config::{
ConfigInput, ConfigOutput, GetLaunchOptionsInput, GetLaunchOptionsOutput,
SetLaunchOptionsInput,
},
error::ReportError,
};
pub async fn listen_config() {
let mut receiver = ConfigInput::get_dart_signal_receiver().unwrap();
@ -12,3 +18,35 @@ pub async fn listen_config() {
.send_signal_to_dart();
}
}
pub async fn listen_get_launch_options() {
let mut receiver = GetLaunchOptionsInput::get_dart_signal_receiver().unwrap();
while let Some(_) = receiver.recv().await {
let launch_options_result = GameConfig::get_launch_options().await;
if let Err(err) = launch_options_result {
ReportError {
error_message: format!("Failed to get launch options :\n{:?}", err),
}
.send_signal_to_dart();
continue;
}
GetLaunchOptionsOutput {
launch_options: launch_options_result.unwrap(),
}
.send_signal_to_dart();
}
}
pub async fn listen_set_launch_options() {
let mut receiver = SetLaunchOptionsInput::get_dart_signal_receiver().unwrap();
while let Some(dart_signal) = receiver.recv().await {
let result = GameConfig::set_launch_options(dart_signal.message.launch_options).await;
if let Err(err) = result {
ReportError {
error_message: format!("Failed to set launch options :\n{:?}", err),
}
.send_signal_to_dart();
}
}
}

View File

@ -16,6 +16,8 @@ async fn main() {
//config
tokio::spawn(game_state::listen_game_state());
tokio::spawn(config::listen_config());
tokio::spawn(config::listen_get_launch_options());
tokio::spawn(config::listen_set_launch_options());
//github
tokio::spawn(github::listen_proton_version());
@ -28,6 +30,6 @@ async fn main() {
tokio::spawn(dependencies::listen_dependecies_installation());
tokio::spawn(game::listen_game_installation());
// game
//game
tokio::spawn(game::listen_game_running());
}