Restrict access to global Audio
(#7122)
This PR restricts access to the `Audio` global to force consumers to go through the `Audio` public interface to interact with it. Release Notes: - N/A
This commit is contained in:
parent
176f63e86e
commit
d97e780135
3 changed files with 18 additions and 15 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -640,6 +640,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"collections",
|
"collections",
|
||||||
|
"derive_more",
|
||||||
"futures 0.3.28",
|
"futures 0.3.28",
|
||||||
"gpui",
|
"gpui",
|
||||||
"log",
|
"log",
|
||||||
|
|
|
@ -10,14 +10,12 @@ path = "src/audio.rs"
|
||||||
doctest = false
|
doctest = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
gpui = { path = "../gpui" }
|
|
||||||
collections = { path = "../collections" }
|
|
||||||
util = { path = "../util" }
|
|
||||||
|
|
||||||
|
|
||||||
rodio ={version = "0.17.1", default-features=false, features = ["wav"]}
|
|
||||||
|
|
||||||
log.workspace = true
|
|
||||||
futures.workspace = true
|
|
||||||
anyhow.workspace = true
|
anyhow.workspace = true
|
||||||
|
collections = { path = "../collections" }
|
||||||
|
derive_more.workspace = true
|
||||||
|
futures.workspace = true
|
||||||
|
gpui = { path = "../gpui" }
|
||||||
|
log.workspace = true
|
||||||
parking_lot.workspace = true
|
parking_lot.workspace = true
|
||||||
|
rodio = { version = "0.17.1", default-features = false, features = ["wav"] }
|
||||||
|
util = { path = "../util" }
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use assets::SoundRegistry;
|
use assets::SoundRegistry;
|
||||||
|
use derive_more::{Deref, DerefMut};
|
||||||
use gpui::{AppContext, AssetSource, Global};
|
use gpui::{AppContext, AssetSource, Global};
|
||||||
use rodio::{OutputStream, OutputStreamHandle};
|
use rodio::{OutputStream, OutputStreamHandle};
|
||||||
use util::ResultExt;
|
use util::ResultExt;
|
||||||
|
@ -7,7 +8,7 @@ mod assets;
|
||||||
|
|
||||||
pub fn init(source: impl AssetSource, cx: &mut AppContext) {
|
pub fn init(source: impl AssetSource, cx: &mut AppContext) {
|
||||||
SoundRegistry::set_global(source, cx);
|
SoundRegistry::set_global(source, cx);
|
||||||
cx.set_global(Audio::new());
|
cx.set_global(GlobalAudio(Audio::new()));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Sound {
|
pub enum Sound {
|
||||||
|
@ -37,7 +38,10 @@ pub struct Audio {
|
||||||
output_handle: Option<OutputStreamHandle>,
|
output_handle: Option<OutputStreamHandle>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Global for Audio {}
|
#[derive(Deref, DerefMut)]
|
||||||
|
struct GlobalAudio(Audio);
|
||||||
|
|
||||||
|
impl Global for GlobalAudio {}
|
||||||
|
|
||||||
impl Audio {
|
impl Audio {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
|
@ -58,11 +62,11 @@ impl Audio {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn play_sound(sound: Sound, cx: &mut AppContext) {
|
pub fn play_sound(sound: Sound, cx: &mut AppContext) {
|
||||||
if !cx.has_global::<Self>() {
|
if !cx.has_global::<GlobalAudio>() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
cx.update_global::<Self, _>(|this, cx| {
|
cx.update_global::<GlobalAudio, _>(|this, cx| {
|
||||||
let output_handle = this.ensure_output_exists()?;
|
let output_handle = this.ensure_output_exists()?;
|
||||||
let source = SoundRegistry::global(cx).get(sound.file()).log_err()?;
|
let source = SoundRegistry::global(cx).get(sound.file()).log_err()?;
|
||||||
output_handle.play_raw(source).log_err()?;
|
output_handle.play_raw(source).log_err()?;
|
||||||
|
@ -71,11 +75,11 @@ impl Audio {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn end_call(cx: &mut AppContext) {
|
pub fn end_call(cx: &mut AppContext) {
|
||||||
if !cx.has_global::<Self>() {
|
if !cx.has_global::<GlobalAudio>() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
cx.update_global::<Self, _>(|this, _| {
|
cx.update_global::<GlobalAudio, _>(|this, _| {
|
||||||
this._output_stream.take();
|
this._output_stream.take();
|
||||||
this.output_handle.take();
|
this.output_handle.take();
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue