Struct playdate_menu::MenuItem
source · pub struct MenuItem<Kind, UserData = (), Api = Default, const REMOVE_ON_DROP: bool = true>(/* private fields */)
where
Kind: Kind,
UserData: Into<Box<UserData>>,
Api: Api;Implementations§
source§impl<UD, K: Kind, Api: Api, const REM: bool> MenuItem<K, UD, Api, REM>
impl<UD, K: Kind, Api: Api, const REM: bool> MenuItem<K, UD, Api, REM>
pub fn get_title(&self) -> Cow<'_, str>
sourcepub fn set_title<S: AsRef<str>>(&self, title: S) -> Result<(), NulError>
pub fn set_title<S: AsRef<str>>(&self, title: S) -> Result<(), NulError>
Examples found in repository?
examples/custom-api-access.rs (line 144)
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
fn change_first(state: &mut Option<&'static State>) {
if let Some(state) = state {
if let Some(item) = state.first.as_ref() {
if let Some(value) = item.get_userdata() {
item.set_title(format!("Clicked: {value}/{MAX_CLICKS}")).unwrap();
} else {
println!("No user-data")
}
} else {
println!("No menu item")
}
} else {
println!("No state")
}
}More examples
examples/menu.rs (line 108)
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
fn change_first(state: &mut Option<&'static State>) {
if let Some(state) = state {
if let Some(item) = state.first.as_ref() {
if let Some(value) = item.get_userdata() {
item.set_title(format!("Clicked: {value}/{MAX_CLICKS}")).unwrap();
} else {
println!("No user-data")
}
} else {
println!("No menu item")
}
} else {
println!("No state")
}
}sourcepub fn get_userdata(&self) -> Option<&mut UD>
pub fn get_userdata(&self) -> Option<&mut UD>
Examples found in repository?
examples/custom-api-access.rs (line 75)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
fn update(&mut self) -> Option<()> {
// remove first menu item if limit reached
if let Some((_item, value)) = self.first
.as_ref()
.map(|item| item.get_userdata().map(|val| (item, val)))
.flatten()
{
if *value >= MAX_CLICKS {
let item = self.first.take().unwrap();
let value = item.remove();
println!("First item removed on click {value:?}");
}
}
// remove third menu item if requested
if let Some((_item, value)) = self.third.as_ref().map(|item| (item, item.selected_option())) {
if value != 0 {
self.third.take();
}
}
const LABEL_DEF: &str = "Use System Menu\0";
let cstr = CStr::from_bytes_with_nul(LABEL_DEF.as_bytes()).unwrap();
unsafe {
let graphics = (*sys::API).graphics;
(*graphics).clear?(LCDSolidColor::kColorWhite as LCDColor);
// get width (screen-size) of text
let text_width = (*graphics).getTextWidth?(
core::ptr::null_mut(),
cstr.as_ptr() as *const _,
LABEL_DEF.len(),
PDStringEncoding::kUTF8Encoding,
0,
);
// render text
(*graphics).drawText?(
cstr.as_ptr() as *const _,
LABEL_DEF.len(),
PDStringEncoding::kUTF8Encoding,
INITIAL_X as c_int - text_width / 2,
INITIAL_Y.try_into().unwrap(),
);
}
Some(())
}
/// Event handler
fn event(&'static mut self, event: PDSystemEvent) -> Option<()> {
match event {
// initial setup
PDSystemEvent::kEventInit => unsafe {
(*(*sys::API).display).setRefreshRate?(20.0);
let api = CustomApi::new();
fn callback(userdata: &mut u32) {
println!("Check menu item clicked {userdata} times.");
*userdata += 1;
}
self.first = SimpleMenuItem::new_with(api.clone(), "Check Me", Some(callback), 0).ok();
fn change_first(state: &mut Option<&'static State>) {
if let Some(state) = state {
if let Some(item) = state.first.as_ref() {
if let Some(value) = item.get_userdata() {
item.set_title(format!("Clicked: {value}/{MAX_CLICKS}")).unwrap();
} else {
println!("No user-data")
}
} else {
println!("No menu item")
}
} else {
println!("No state")
}
}More examples
examples/menu.rs (line 43)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
fn update(&mut self) -> Option<()> {
// remove first menu item if limit reached
if let Some((_item, value)) = self.first
.as_ref()
.map(|item| item.get_userdata().map(|val| (item, val)))
.flatten()
{
if *value >= MAX_CLICKS {
let item = self.first.take().unwrap();
let value = item.remove();
println!("First item removed on click {value:?}");
}
}
// remove third menu item if requested
if let Some((_item, value)) = self.third.as_ref().map(|item| (item, item.selected_option())) {
if value != 0 {
self.third.take();
}
}
const LABEL_DEF: &str = "Use System Menu\0";
let cstr = CStr::from_bytes_with_nul(LABEL_DEF.as_bytes()).unwrap();
unsafe {
let graphics = (*sys::API).graphics;
(*graphics).clear?(LCDSolidColor::kColorWhite as LCDColor);
// get width (screen-size) of text
let text_width = (*graphics).getTextWidth?(
core::ptr::null_mut(),
cstr.as_ptr() as *const _,
LABEL_DEF.len(),
PDStringEncoding::kUTF8Encoding,
0,
);
// render text
(*graphics).drawText?(
cstr.as_ptr() as *const _,
LABEL_DEF.len(),
PDStringEncoding::kUTF8Encoding,
INITIAL_X as c_int - text_width / 2,
INITIAL_Y.try_into().unwrap(),
);
}
Some(())
}
/// Event handler
fn event(&'static mut self, event: PDSystemEvent) -> Option<()> {
match event {
// initial setup
PDSystemEvent::kEventInit => unsafe {
(*(*sys::API).display).setRefreshRate?(20.0);
fn callback(userdata: &mut u32) {
println!("Check menu item clicked {userdata} times.");
*userdata += 1;
}
self.first = SimpleMenuItem::new("Check Me", Some(callback), 0).unwrap().into();
fn change_first(state: &mut Option<&'static State>) {
if let Some(state) = state {
if let Some(item) = state.first.as_ref() {
if let Some(value) = item.get_userdata() {
item.set_title(format!("Clicked: {value}/{MAX_CLICKS}")).unwrap();
} else {
println!("No user-data")
}
} else {
println!("No menu item")
}
} else {
println!("No state")
}
}sourcepub fn set_userdata(&self, userdata: UD) -> Option<UD>
pub fn set_userdata(&self, userdata: UD) -> Option<UD>
Examples found in repository?
examples/menu.rs (line 122)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
fn event(&'static mut self, event: PDSystemEvent) -> Option<()> {
match event {
// initial setup
PDSystemEvent::kEventInit => unsafe {
(*(*sys::API).display).setRefreshRate?(20.0);
fn callback(userdata: &mut u32) {
println!("Check menu item clicked {userdata} times.");
*userdata += 1;
}
self.first = SimpleMenuItem::new("Check Me", Some(callback), 0).unwrap().into();
fn change_first(state: &mut Option<&'static State>) {
if let Some(state) = state {
if let Some(item) = state.first.as_ref() {
if let Some(value) = item.get_userdata() {
item.set_title(format!("Clicked: {value}/{MAX_CLICKS}")).unwrap();
} else {
println!("No user-data")
}
} else {
println!("No menu item")
}
} else {
println!("No state")
}
}
self.second = CheckMenuItem::new("Change^", false, Some(change_first), None).unwrap()
.into();
let second = self.second.as_ref().unwrap();
second.set_userdata(Some(self));
self.third = OptionsMenuItem::new("Remove?", ["No", "Yes"], None, ()).unwrap()
.into();
},
_ => {},
}
Some(())
}More examples
examples/custom-api-access.rs (line 159)
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
fn event(&'static mut self, event: PDSystemEvent) -> Option<()> {
match event {
// initial setup
PDSystemEvent::kEventInit => unsafe {
(*(*sys::API).display).setRefreshRate?(20.0);
let api = CustomApi::new();
fn callback(userdata: &mut u32) {
println!("Check menu item clicked {userdata} times.");
*userdata += 1;
}
self.first = SimpleMenuItem::new_with(api.clone(), "Check Me", Some(callback), 0).ok();
fn change_first(state: &mut Option<&'static State>) {
if let Some(state) = state {
if let Some(item) = state.first.as_ref() {
if let Some(value) = item.get_userdata() {
item.set_title(format!("Clicked: {value}/{MAX_CLICKS}")).unwrap();
} else {
println!("No user-data")
}
} else {
println!("No menu item")
}
} else {
println!("No state")
}
}
self.second =
CheckMenuItem::new_with(api.clone(), "Change^", false, Some(change_first), None).unwrap()
.into();
let second = self.second.as_ref().unwrap();
second.set_userdata(Some(self));
self.third = OptionsMenuItem::new_with(api.clone(), "Remove?", ["No", "Yes"], None, ()).unwrap()
.into();
},
_ => {},
}
Some(())
}pub fn get_value(&self) -> c_int
pub fn set_value(&self, value: c_int)
source§impl<UD: Sized, Api: Api, const REM: bool> MenuItem<Check, UD, Api, REM>
impl<UD: Sized, Api: Api, const REM: bool> MenuItem<Check, UD, Api, REM>
sourcepub fn new<S: AsRef<str>>(
title: S,
checked: bool,
callback: Option<fn(userdata: &mut UD)>,
userdata: UD
) -> Result<Self, ApiError>where
Api: Default,
pub fn new<S: AsRef<str>>( title: S, checked: bool, callback: Option<fn(userdata: &mut UD)>, userdata: UD ) -> Result<Self, ApiError>where Api: Default,
Examples found in repository?
examples/menu.rs (line 119)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
fn event(&'static mut self, event: PDSystemEvent) -> Option<()> {
match event {
// initial setup
PDSystemEvent::kEventInit => unsafe {
(*(*sys::API).display).setRefreshRate?(20.0);
fn callback(userdata: &mut u32) {
println!("Check menu item clicked {userdata} times.");
*userdata += 1;
}
self.first = SimpleMenuItem::new("Check Me", Some(callback), 0).unwrap().into();
fn change_first(state: &mut Option<&'static State>) {
if let Some(state) = state {
if let Some(item) = state.first.as_ref() {
if let Some(value) = item.get_userdata() {
item.set_title(format!("Clicked: {value}/{MAX_CLICKS}")).unwrap();
} else {
println!("No user-data")
}
} else {
println!("No menu item")
}
} else {
println!("No state")
}
}
self.second = CheckMenuItem::new("Change^", false, Some(change_first), None).unwrap()
.into();
let second = self.second.as_ref().unwrap();
second.set_userdata(Some(self));
self.third = OptionsMenuItem::new("Remove?", ["No", "Yes"], None, ()).unwrap()
.into();
},
_ => {},
}
Some(())
}sourcepub fn new_with<S: AsRef<str>>(
api: Api,
title: S,
checked: bool,
callback: Option<fn(userdata: &mut UD)>,
userdata: UD
) -> Result<Self, ApiError>
pub fn new_with<S: AsRef<str>>( api: Api, title: S, checked: bool, callback: Option<fn(userdata: &mut UD)>, userdata: UD ) -> Result<Self, ApiError>
Examples found in repository?
examples/custom-api-access.rs (line 156)
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
fn event(&'static mut self, event: PDSystemEvent) -> Option<()> {
match event {
// initial setup
PDSystemEvent::kEventInit => unsafe {
(*(*sys::API).display).setRefreshRate?(20.0);
let api = CustomApi::new();
fn callback(userdata: &mut u32) {
println!("Check menu item clicked {userdata} times.");
*userdata += 1;
}
self.first = SimpleMenuItem::new_with(api.clone(), "Check Me", Some(callback), 0).ok();
fn change_first(state: &mut Option<&'static State>) {
if let Some(state) = state {
if let Some(item) = state.first.as_ref() {
if let Some(value) = item.get_userdata() {
item.set_title(format!("Clicked: {value}/{MAX_CLICKS}")).unwrap();
} else {
println!("No user-data")
}
} else {
println!("No menu item")
}
} else {
println!("No state")
}
}
self.second =
CheckMenuItem::new_with(api.clone(), "Change^", false, Some(change_first), None).unwrap()
.into();
let second = self.second.as_ref().unwrap();
second.set_userdata(Some(self));
self.third = OptionsMenuItem::new_with(api.clone(), "Remove?", ["No", "Yes"], None, ()).unwrap()
.into();
},
_ => {},
}
Some(())
}source§impl<UD: Sized, Api: Api, const REM: bool> MenuItem<Simple, UD, Api, REM>
impl<UD: Sized, Api: Api, const REM: bool> MenuItem<Simple, UD, Api, REM>
sourcepub fn new<S: AsRef<str>>(
title: S,
callback: Option<fn(userdata: &mut UD)>,
userdata: UD
) -> Result<Self, ApiError>where
Api: Default,
pub fn new<S: AsRef<str>>( title: S, callback: Option<fn(userdata: &mut UD)>, userdata: UD ) -> Result<Self, ApiError>where Api: Default,
Examples found in repository?
examples/menu.rs (line 101)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
fn event(&'static mut self, event: PDSystemEvent) -> Option<()> {
match event {
// initial setup
PDSystemEvent::kEventInit => unsafe {
(*(*sys::API).display).setRefreshRate?(20.0);
fn callback(userdata: &mut u32) {
println!("Check menu item clicked {userdata} times.");
*userdata += 1;
}
self.first = SimpleMenuItem::new("Check Me", Some(callback), 0).unwrap().into();
fn change_first(state: &mut Option<&'static State>) {
if let Some(state) = state {
if let Some(item) = state.first.as_ref() {
if let Some(value) = item.get_userdata() {
item.set_title(format!("Clicked: {value}/{MAX_CLICKS}")).unwrap();
} else {
println!("No user-data")
}
} else {
println!("No menu item")
}
} else {
println!("No state")
}
}
self.second = CheckMenuItem::new("Change^", false, Some(change_first), None).unwrap()
.into();
let second = self.second.as_ref().unwrap();
second.set_userdata(Some(self));
self.third = OptionsMenuItem::new("Remove?", ["No", "Yes"], None, ()).unwrap()
.into();
},
_ => {},
}
Some(())
}sourcepub fn new_with<S: AsRef<str>>(
api: Api,
title: S,
callback: Option<fn(userdata: &mut UD)>,
userdata: UD
) -> Result<Self, ApiError>
pub fn new_with<S: AsRef<str>>( api: Api, title: S, callback: Option<fn(userdata: &mut UD)>, userdata: UD ) -> Result<Self, ApiError>
Examples found in repository?
examples/custom-api-access.rs (line 137)
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
fn event(&'static mut self, event: PDSystemEvent) -> Option<()> {
match event {
// initial setup
PDSystemEvent::kEventInit => unsafe {
(*(*sys::API).display).setRefreshRate?(20.0);
let api = CustomApi::new();
fn callback(userdata: &mut u32) {
println!("Check menu item clicked {userdata} times.");
*userdata += 1;
}
self.first = SimpleMenuItem::new_with(api.clone(), "Check Me", Some(callback), 0).ok();
fn change_first(state: &mut Option<&'static State>) {
if let Some(state) = state {
if let Some(item) = state.first.as_ref() {
if let Some(value) = item.get_userdata() {
item.set_title(format!("Clicked: {value}/{MAX_CLICKS}")).unwrap();
} else {
println!("No user-data")
}
} else {
println!("No menu item")
}
} else {
println!("No state")
}
}
self.second =
CheckMenuItem::new_with(api.clone(), "Change^", false, Some(change_first), None).unwrap()
.into();
let second = self.second.as_ref().unwrap();
second.set_userdata(Some(self));
self.third = OptionsMenuItem::new_with(api.clone(), "Remove?", ["No", "Yes"], None, ()).unwrap()
.into();
},
_ => {},
}
Some(())
}source§impl<UD: Sized, Api: Api, const REM: bool> MenuItem<Options, UD, Api, REM>
impl<UD: Sized, Api: Api, const REM: bool> MenuItem<Options, UD, Api, REM>
sourcepub fn new<S: AsRef<str>, O: AsRef<[S]>>(
title: S,
options: O,
callback: Option<fn(userdata: &mut UD)>,
userdata: UD
) -> Result<Self, ApiError>where
Api: Default,
pub fn new<S: AsRef<str>, O: AsRef<[S]>>( title: S, options: O, callback: Option<fn(userdata: &mut UD)>, userdata: UD ) -> Result<Self, ApiError>where Api: Default,
Examples found in repository?
examples/menu.rs (line 125)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
fn event(&'static mut self, event: PDSystemEvent) -> Option<()> {
match event {
// initial setup
PDSystemEvent::kEventInit => unsafe {
(*(*sys::API).display).setRefreshRate?(20.0);
fn callback(userdata: &mut u32) {
println!("Check menu item clicked {userdata} times.");
*userdata += 1;
}
self.first = SimpleMenuItem::new("Check Me", Some(callback), 0).unwrap().into();
fn change_first(state: &mut Option<&'static State>) {
if let Some(state) = state {
if let Some(item) = state.first.as_ref() {
if let Some(value) = item.get_userdata() {
item.set_title(format!("Clicked: {value}/{MAX_CLICKS}")).unwrap();
} else {
println!("No user-data")
}
} else {
println!("No menu item")
}
} else {
println!("No state")
}
}
self.second = CheckMenuItem::new("Change^", false, Some(change_first), None).unwrap()
.into();
let second = self.second.as_ref().unwrap();
second.set_userdata(Some(self));
self.third = OptionsMenuItem::new("Remove?", ["No", "Yes"], None, ()).unwrap()
.into();
},
_ => {},
}
Some(())
}sourcepub fn new_with<S: AsRef<str>, O: AsRef<[S]>>(
api: Api,
title: S,
options: O,
callback: Option<fn(userdata: &mut UD)>,
userdata: UD
) -> Result<Self, ApiError>
pub fn new_with<S: AsRef<str>, O: AsRef<[S]>>( api: Api, title: S, options: O, callback: Option<fn(userdata: &mut UD)>, userdata: UD ) -> Result<Self, ApiError>
Examples found in repository?
examples/custom-api-access.rs (line 162)
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
fn event(&'static mut self, event: PDSystemEvent) -> Option<()> {
match event {
// initial setup
PDSystemEvent::kEventInit => unsafe {
(*(*sys::API).display).setRefreshRate?(20.0);
let api = CustomApi::new();
fn callback(userdata: &mut u32) {
println!("Check menu item clicked {userdata} times.");
*userdata += 1;
}
self.first = SimpleMenuItem::new_with(api.clone(), "Check Me", Some(callback), 0).ok();
fn change_first(state: &mut Option<&'static State>) {
if let Some(state) = state {
if let Some(item) = state.first.as_ref() {
if let Some(value) = item.get_userdata() {
item.set_title(format!("Clicked: {value}/{MAX_CLICKS}")).unwrap();
} else {
println!("No user-data")
}
} else {
println!("No menu item")
}
} else {
println!("No state")
}
}
self.second =
CheckMenuItem::new_with(api.clone(), "Change^", false, Some(change_first), None).unwrap()
.into();
let second = self.second.as_ref().unwrap();
second.set_userdata(Some(self));
self.third = OptionsMenuItem::new_with(api.clone(), "Remove?", ["No", "Yes"], None, ()).unwrap()
.into();
},
_ => {},
}
Some(())
}source§impl<UD, Api: Api, const REM: bool> MenuItem<Check, UD, Api, REM>
impl<UD, Api: Api, const REM: bool> MenuItem<Check, UD, Api, REM>
pub fn is_checked(&self) -> bool
source§impl<UD, Api: Api, const REM: bool> MenuItem<Options, UD, Api, REM>
impl<UD, Api: Api, const REM: bool> MenuItem<Options, UD, Api, REM>
sourcepub fn selected_option(&self) -> i32
pub fn selected_option(&self) -> i32
The array index of the currently selected option.
Examples found in repository?
examples/custom-api-access.rs (line 86)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
fn update(&mut self) -> Option<()> {
// remove first menu item if limit reached
if let Some((_item, value)) = self.first
.as_ref()
.map(|item| item.get_userdata().map(|val| (item, val)))
.flatten()
{
if *value >= MAX_CLICKS {
let item = self.first.take().unwrap();
let value = item.remove();
println!("First item removed on click {value:?}");
}
}
// remove third menu item if requested
if let Some((_item, value)) = self.third.as_ref().map(|item| (item, item.selected_option())) {
if value != 0 {
self.third.take();
}
}
const LABEL_DEF: &str = "Use System Menu\0";
let cstr = CStr::from_bytes_with_nul(LABEL_DEF.as_bytes()).unwrap();
unsafe {
let graphics = (*sys::API).graphics;
(*graphics).clear?(LCDSolidColor::kColorWhite as LCDColor);
// get width (screen-size) of text
let text_width = (*graphics).getTextWidth?(
core::ptr::null_mut(),
cstr.as_ptr() as *const _,
LABEL_DEF.len(),
PDStringEncoding::kUTF8Encoding,
0,
);
// render text
(*graphics).drawText?(
cstr.as_ptr() as *const _,
LABEL_DEF.len(),
PDStringEncoding::kUTF8Encoding,
INITIAL_X as c_int - text_width / 2,
INITIAL_Y.try_into().unwrap(),
);
}
Some(())
}More examples
examples/menu.rs (line 54)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
fn update(&mut self) -> Option<()> {
// remove first menu item if limit reached
if let Some((_item, value)) = self.first
.as_ref()
.map(|item| item.get_userdata().map(|val| (item, val)))
.flatten()
{
if *value >= MAX_CLICKS {
let item = self.first.take().unwrap();
let value = item.remove();
println!("First item removed on click {value:?}");
}
}
// remove third menu item if requested
if let Some((_item, value)) = self.third.as_ref().map(|item| (item, item.selected_option())) {
if value != 0 {
self.third.take();
}
}
const LABEL_DEF: &str = "Use System Menu\0";
let cstr = CStr::from_bytes_with_nul(LABEL_DEF.as_bytes()).unwrap();
unsafe {
let graphics = (*sys::API).graphics;
(*graphics).clear?(LCDSolidColor::kColorWhite as LCDColor);
// get width (screen-size) of text
let text_width = (*graphics).getTextWidth?(
core::ptr::null_mut(),
cstr.as_ptr() as *const _,
LABEL_DEF.len(),
PDStringEncoding::kUTF8Encoding,
0,
);
// render text
(*graphics).drawText?(
cstr.as_ptr() as *const _,
LABEL_DEF.len(),
PDStringEncoding::kUTF8Encoding,
INITIAL_X as c_int - text_width / 2,
INITIAL_Y.try_into().unwrap(),
);
}
Some(())
}source§impl<UD, K: Kind, Api: Api, const REM: bool> MenuItem<K, UD, Api, REM>
impl<UD, K: Kind, Api: Api, const REM: bool> MenuItem<K, UD, Api, REM>
sourcepub fn remove(self) -> Option<UD>
pub fn remove(self) -> Option<UD>
Examples found in repository?
examples/custom-api-access.rs (line 80)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
fn update(&mut self) -> Option<()> {
// remove first menu item if limit reached
if let Some((_item, value)) = self.first
.as_ref()
.map(|item| item.get_userdata().map(|val| (item, val)))
.flatten()
{
if *value >= MAX_CLICKS {
let item = self.first.take().unwrap();
let value = item.remove();
println!("First item removed on click {value:?}");
}
}
// remove third menu item if requested
if let Some((_item, value)) = self.third.as_ref().map(|item| (item, item.selected_option())) {
if value != 0 {
self.third.take();
}
}
const LABEL_DEF: &str = "Use System Menu\0";
let cstr = CStr::from_bytes_with_nul(LABEL_DEF.as_bytes()).unwrap();
unsafe {
let graphics = (*sys::API).graphics;
(*graphics).clear?(LCDSolidColor::kColorWhite as LCDColor);
// get width (screen-size) of text
let text_width = (*graphics).getTextWidth?(
core::ptr::null_mut(),
cstr.as_ptr() as *const _,
LABEL_DEF.len(),
PDStringEncoding::kUTF8Encoding,
0,
);
// render text
(*graphics).drawText?(
cstr.as_ptr() as *const _,
LABEL_DEF.len(),
PDStringEncoding::kUTF8Encoding,
INITIAL_X as c_int - text_width / 2,
INITIAL_Y.try_into().unwrap(),
);
}
Some(())
}More examples
examples/menu.rs (line 48)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
fn update(&mut self) -> Option<()> {
// remove first menu item if limit reached
if let Some((_item, value)) = self.first
.as_ref()
.map(|item| item.get_userdata().map(|val| (item, val)))
.flatten()
{
if *value >= MAX_CLICKS {
let item = self.first.take().unwrap();
let value = item.remove();
println!("First item removed on click {value:?}");
}
}
// remove third menu item if requested
if let Some((_item, value)) = self.third.as_ref().map(|item| (item, item.selected_option())) {
if value != 0 {
self.third.take();
}
}
const LABEL_DEF: &str = "Use System Menu\0";
let cstr = CStr::from_bytes_with_nul(LABEL_DEF.as_bytes()).unwrap();
unsafe {
let graphics = (*sys::API).graphics;
(*graphics).clear?(LCDSolidColor::kColorWhite as LCDColor);
// get width (screen-size) of text
let text_width = (*graphics).getTextWidth?(
core::ptr::null_mut(),
cstr.as_ptr() as *const _,
LABEL_DEF.len(),
PDStringEncoding::kUTF8Encoding,
0,
);
// render text
(*graphics).drawText?(
cstr.as_ptr() as *const _,
LABEL_DEF.len(),
PDStringEncoding::kUTF8Encoding,
INITIAL_X as c_int - text_width / 2,
INITIAL_Y.try_into().unwrap(),
);
}
Some(())
}Trait Implementations§
Auto Trait Implementations§
impl<Kind, UserData, Api, const REMOVE_ON_DROP: bool> RefUnwindSafe for MenuItem<Kind, UserData, Api, REMOVE_ON_DROP>where Api: RefUnwindSafe, Kind: RefUnwindSafe, UserData: RefUnwindSafe,
impl<Kind, UserData = (), Api = Default, const REMOVE_ON_DROP: bool = true> !Send for MenuItem<Kind, UserData, Api, REMOVE_ON_DROP>
impl<Kind, UserData = (), Api = Default, const REMOVE_ON_DROP: bool = true> !Sync for MenuItem<Kind, UserData, Api, REMOVE_ON_DROP>
impl<Kind, UserData, Api, const REMOVE_ON_DROP: bool> Unpin for MenuItem<Kind, UserData, Api, REMOVE_ON_DROP>where Api: Unpin, Kind: Unpin, UserData: Unpin,
impl<Kind, UserData, Api, const REMOVE_ON_DROP: bool> UnwindSafe for MenuItem<Kind, UserData, Api, REMOVE_ON_DROP>where Api: UnwindSafe, Kind: UnwindSafe, UserData: UnwindSafe,
Blanket Implementations§
§impl<T> Any for Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere T: 'static + ?Sized,
§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere T: ?Sized,
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<T, U> Into<U> for Twhere
U: From<T>,
impl<T, U> Into<U> for Twhere U: From<T>,
§impl<T, U> TryFrom<U> for Twhere
U: Into<T>,
impl<T, U> TryFrom<U> for Twhere U: Into<T>,
Layout§
Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.